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

nginx常用的配置解析

程序员文章站 2022-03-10 23:27:26
...

nginx相关配置

1、nginx代理文件路径

 location /file {
     root   /home/output/data;
     rewrite ^/file/(.*)$ /file/$1 break;
     proxy_http_version 1.1;
 }

也可以这样

location /file {
    alias  '/home/output/data/file/';
    allow all;
    autoindex on;
}

调用 http://ip:port/file就可以访问到file下的文件

2.NGINX代理跨域请求:

location /apis {
    rewrite  ^.+apis/(.*)$ /$1 break;
    proxy_pass   http://127.0.0.1:8080/;
    add_header Access-Control-Allow-Origin *;
    add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
    add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
    proxy_set_header X-Real_IP $remote_addr;   # 拿到真实的ip
    proxy_set_header X-Forwarded-For $remote_addr:$remote_port; # 拿到真实的ip和端口
    proxy_http_version 1.1;
}

3.nginx配置https(注意需要开启443端口)

HTTPS server

  • 需要把下载下来的CA证书和**上传到nginx下的config目录,也就是和nginx.config在同一目录(方便后面配置)
 server {
        server_name  www.test.cn; // 服务器绑定的域名
        listen       443 ssl;
        ssl_certificate      3178497_www.test.cn.pem;  //ca证书存放的路径
        ssl_certificate_key  3178497_www.test.cn.key;//ca**存放的路径
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_prefer_server_ciphers  on;

		
        location /apis {
             rewrite  ^.+apis/(.*)$ /$1 break;
            proxy_pass   http://127.0.0.1:8080/;
            add_header Access-Control-Allow-Origin *;
            add_header Access-Control-Allow-Headers "Origin, X-Requested-With, Content-Type, Accept";
            add_header Access-Control-Allow-Methods "GET, POST, OPTIONS";
            proxy_http_version 1.1;
        }
		
		location /web {
            root   /home/opt/web/video_web/dist;
            index  index.html index.htm;
        }
		#文件代理
		location /file {
			root   /home/output/data/file;
			proxy_http_version 1.1;
		}
		
    }

4.nginx配置监听websocket(https安全连接也是这样代理)

location /ws {
    rewrite ^.+ws/(.*)$ /$1 break;
    proxy_redirect off;
    proxy_pass http://127.0.0.1:8088;
    proxy_set_header Host $host;
    proxy_set_header X-Real_IP $remote_addr;   # 拿到真实的ip
    proxy_set_header X-Forwarded-For $remote_addr:$remote_port; # 拿到真实的ip和端口
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;   # 升级协议头
   proxy_set_header Connection upgrade;
   proxy_read_timeout 600s; # 配置连接超时时间,10分钟内没有数据交互会断开连接
}

结合前端发送心跳就可以保持一直连接了。

使用的时候是:
wss://域名(或者ip:port)/ws/websocket/

5、nginx配置负载均衡

需要在http里面添加

upstream webservers {
        server 172.18.144.23:4789 weight=10;
        server 172.18.144.23:5789 weight=10;
    }

然后把server改为

server {
        listen       80;
        server_name  test.nginx.com;
        
        location / {
            proxy_pass   http://webservers;
        }   
             
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        
    }