Nginx

Nginx 常用配置


#user  nobody;
worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        root   html;

        #charset koi8-r;
        #access_log  logs/host.access.log  main;

        location / {
            try_files $uri /index.html;
            index  index.html index.htm;
        }

        location /login {
            try_files $uri /login/index.html; # try_files 地址是相对于 root 的地址
            index  index.html index.htm;
        }

        location /iconify {
            rewrite ^/iconify/(.*)$ /$1 break;
            proxy_pass http://172.21.44.57:3000;
        }

        location /api/graphql {
            proxy_pass http://172.21.44.120:6543;
        }

        location /api/model/graphql {
            rewrite ^/api/model/graphql$ /api/graphql/model break; # 不能省略 /api
            proxy_pass http://172.21.44.57:3000;
        }
        
        # WebSocket代理
        location /wsUrl/ {
            rewrite ^/wsUrl/(.*)$ /$1 break;       # 拦截标识去除
            proxy_pass http://192.168.100.20:8080; # 这里是 http 不是 ws ,代理的 ip 和 port 写 ws 访问的实际地址
            proxy_http_version 1.1;                # 这里必须使用 http 1.1
            # 下面两个必须设置,请求头设置为ws请求方式
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }

        #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;
        }
    }

    server {
        listen      8080;
        server_name  cruise.wudandong.top;

        #charset koi8-r;

        location / {  # 访问以 / 访问,没有 cruise 前缀,只是改变静态资源路径
            root   html/cruise;
            try_files $uri $uri/ /index.html; 
            index  index.html index.htm;
        }

        location /api/ {
            rewrite ^/api/(.*)$ /$1 break;  # ^/api/(.*)$ /api/$1 break; 是否保留 api
            proxy_pass http://47.107.90.142:3001/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    server {
        listen       8000;
        server_name  localhost;
        root /dist

        # 用 app2 前缀访问二级目录
        location /app2 {
            alias   /dist/app-vue2; # 如果 build 的时候 base 是 app2 , 但是部署的物理路径也是 /dist/app-vue2 , 可以使用 alias
            try_files $uri /app-vue2/index.html; # try_files 始终是相对于 root 的物理路径
            index  index.html index.htm;
            add_header Access-Control-Allow-Origin *; # 跨域
            add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, DELETE';
            add_header Access-Control-Allow-Headers 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
        }
    }
}
  • try_filesroot 为相对路径进行查找;

  • aliasroot 指令在功能上相似,但它们在处理 URI 时的方式不同。 root 指令会将 location 块中指定的 URI 附加到指定的根路径后面来查找文件,而 alias 则会替换掉 location 块中的 URI;

  • alias 相当于给物理路径起别名,一般用于 location 路径和真实物理路径不一致的情况;

命令

nginx -v

nginx -t  # 查看配置文件路径

# 重启
nginx -s reload

# 停止
nginx -s stop

mac 系统中的路径

# 配置文件路径
/opt/homebrew/etc/nginx/servers/

# html 默认路径
/opt/homebrew/var/www

ubuntu 中的路径

# 配置文件路径 , 一般在 conf.d 文件夹中配置
/etc/nginx  

# html 默认路径
/var/www

代理路径相对绝对影响

proxy_pass 和 location 结尾的 / ,要么都有,要么都没有。 尽量配合 rewrite ^/-/iconify/(.*)$ /$1 break;

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080/proxy/cuffs/css/toosimple.txt

location /proxy {
    proxy_pass http://10.0.0.1:8080;
}

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080//cuffs/css/toosimple.txt

location /proxy {
    proxy_pass http://10.0.0.1:8080/;
}

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080/cuffs/css/toosimple.txt

location /proxy/ {
    proxy_pass http://10.0.0.1:8080/;
}

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080/proxy/cuffs/css/toosimple.txt

location /proxy/ {
    proxy_pass http://10.0.0.1:8080;
}

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080/static01/cuffs/css/toosimple.txt

location /proxy/ {
    proxy_pass http://10.0.0.1:8080/static01/;
}

# http://127.0.0.1/proxy/cuffs/css/toosimple.txt -> 
# http://10.0.0.1:8080/static01cuffs/css/toosimple.txt

location /proxy/ {
    proxy_pass http://10.0.0.1:8080/static01;
}

localtion 规则

location /documents/            # 匹配任何以 /documents/ 开头的地址
location ~ /documents/abc       # 匹配任何以/documents/abc开头的地址
location ^~ /images/            # 以/images/开头的地址,匹配符合后,停止往下匹配
location ~*\.(gif|jpg|gpeg)$    # 匹配所有以 gif, jpg或jpeg结尾的请求

静态服务

http {
    autoindex on; # 自动索引

    server {
        listen   3000; # 静态服务
        server_name  localhost;
        location / {
            root D:\\nginx-static;
        }
    }
}