欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

nginx相关配置及相关命令

程序员文章站 2022-06-03 08:39:10
...

前注:以下配置都是以目前vue或react前端框架打包为dist文件后的配置,如果您的前端代码不是这样,可以参考修改相关配置名称或相关地址名称

1、常用配置

以下代码为nginx的常用配置,可以直接配置相关服务、服务器上的地址、nginx日志地址及代理地址。

user  root;
worker_processes  1;

events {
    worker_connections  1024;
}

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

    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;
        root /usr/front/dist;         

        location / {
            try_files $uri $uri/ /index.html;
            add_header Cache-Control no-store;
            add_header 'Access-Control-Allow-Origin' '*';
            proxy_set_header Host $host:$server_port;
            proxy_set_header x-forwarded-for  $remote_addr;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            client_max_body_size 100m;
            client_body_buffer_size 50m;
            client_body_temp_path /usr/local/nginx/nginx_temp;
            index  index.html index.htm;
        }
        location ^~ /api/ {
            proxy_pass http://127.0.0.1:8000/;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2、双代理配置

以下只有主要的代理配置,即一个端口代理两个地址。
注意:配置时,前端框架代码也需要做相关的配置
前端框架配置主要是打包后使用history模式,同时设置同base值即可,以下列子base值分别为’project-front’和’project-backend’
root 处地址是服务器上需要代理的两个打包文件的共同位置
try_files 处地址是排除在root地址后,需要代理的两个打包文件的具体位置

server {
        listen       80;
        server_name  localhost;
        charset utf-8;

        root /usr/front;

        location /project-front {
            try_files $uri $uri/ /project/dist/index.html;
            add_header Cache-Control no-store;
            add_header 'Access-Control-Allow-Origin' '*';
            proxy_set_header Host $host:$server_port;
            proxy_set_header x-forwarded-for  $remote_addr;
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            client_max_body_size 100m;
            client_body_buffer_size 50m;
            client_body_temp_path  /usr/local/nginx/nginx_temp;
            index  index.html index.htm;
        }

        location /project-backend {
            try_files $uri $uri/ /projectBakend/dist/index.html;
            add_header Cache-Control no-store;
            add_header 'Access-Control-Allow-Origin' '*';
            proxy_set_header Host $host:$server_port;
            proxy_set_header x-forwarded-for  $remote_addr;
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            client_max_body_size 100m;
            client_body_buffer_size 50m;
            client_body_temp_path  /usr/local/nginx/nginx_temp;
            index  index.html index.htm;
        }

        location ^~ /api/ {
            proxy_pass http://127.0.0.1:8000/;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   Host              $http_host;
            proxy_set_header   X-Real-IP         $remote_addr;
        }
	
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

3、nginx常用命令

1、检查配置文件是否配置成功,在启动服务之前执行

nginx -t

2、启动运行nginx服务;

start nginx

3、等任务完成之后,关闭Nginx服务;

nginx -s quit

4、强制关闭Nginx服务

nginx -s stop

5、重启Nginx服务,一般是修改了配置文件之后,使修改生效

nginx地址/nginx -s reload
或者
sudo service nginx restart(权限不够才加上sudo,如果是root权限则可以不加)

相关标签: 服务器