云迈博客

您现在的位置是:首页 > 灌水专栏 > 正文

灌水专栏

Nginx 的安装与配置

wsinbol2022-07-17灌水专栏219
场景描述Nginx的作用有很多,今天主要讨论下安装和反向代理。之前Nginx安装都是通过宝塔一键安装,反向代理也是在宝塔面板配置的。直到某个项目也做反向代理时,却发现无论怎么配置都是跨域,资料找的

场景描述

Nginx 的作用有很多,今天主要讨论下安装和反向代理。之前Nginx 安装都是通过宝塔一键安装,反向代理也是在宝塔面板配置的。直到某个项目也做反向代理时,却发现无论怎么配置都是跨域,资料找的我人都麻了!于是想到脱离宝塔,自己安装看看效果。事实证明,后面再也不想用宝塔了!

下载安装

Nginx 的下载地址,分Linux和Windows两个版本。

Windows下的安装与各种命令

Windows系统安装 Nginx 十分简单。不需要编译,也没有引导安装程序。解压后便可使用。

start nginx # 启动 Nginx
nginx -s reload # 重载配置
nginx -s stop # 停止 Nginx

Linux下的安装与各种命令

  • 安装依赖
# 安装gcc、gcc-c++
yum -y install gcc
yum -y install gcc-c++

# 安装pcre 、zilb
yum -y install pcre*
yum -y install zlib*

# 安装openssl(若需要支持 https 协议)
yum -y install openssl
yum -y install openssl-devel
  • 编译配置

./configure --prefix=/usr/local/nginx --with-http_ssl_module --with-http_stub_status_module --with-pcre

注: –prefix:设置安装路径。 –with-http_stub_status_module:支持nginx状态查询。 –with-http_ssl_module:支持https。 –with-pcre:为了支持rewrite重写功能,必须制定pcre。

  • 编译安装

make && make install

  • 指定配置文件启动

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

  • 其他命令
cd /usr/local/nginx/sbin # 进入nginx配置
./nginx -t # 查看配置是否正确
./nginx -s reload # 重载配置

配置反向代理


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {

        listen       8080;
        server_name  localhost;

        location / {
            add_header Access-Control-Allow-Origin '*';
            add_header Access-Control-Allow-Methods '*';
            add_header Access-Control-Allow-Headers '*';

            if ($request_method = 'OPTIONS') {
                return 204;
            }
            root   html;
            index  index.html index.htm;
            add_header 'Access-Control-Allow-Origin' '*';
        }

        location /server {
            add_header Access-Control-Allow-Origin '*';
            add_header Access-Control-Allow-Methods '*';
            add_header Access-Control-Allow-Headers '*';

            if ($request_method = 'OPTIONS') {
                return 204;
            }

            rewrite ^.+server/?(.*)$ /$1 break;
            include uwsgi_params;
            proxy_pass 目标URL;
        }

        # redirect server error pages to the static page /50x.html

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
            add_header 'Access-Control-Allow-Origin' '*';
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

常见问题

Q: Vue打包后的项目部署在自己搭建的Nginx上,刷新404问题

A: 见下述代码:

location / {
    root   /home/tz/data/web;
    try_files $uri $uri/ @router;
    index  index.html index.htm;
}
location @router{
    rewrite ^.*$ /index.html last;
}

Q: nginx 开机启动配置

A: 在 cr.local 中加入启动命令 echo "/usr/local/nginx/sbin/nginx" >> /etc/rc.local

发表评论

评论列表

  • 这篇文章还没有收到评论,赶紧来抢沙发吧~