nginx配置根据规则匹配路由

复杂转发设置

一般nginx配置路径,各种服务如没有做到合理规划,就会出来各种情况,难以维护.
不过幸好,nginx功能强大,可以支持各种过滤.
以下对配置信息做以说明:

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;//超时时间
    gzip  on;//gzip压缩
    gzip_min_length 1024;
    gzip_comp_level 5;
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

	server {
          listen 80;
          server_name  test.com;

           location ~* "(sse)" {//过滤请求包含sse,代理到1001端口
            proxy_pass http://127.0.0.1:1001;
            index  index.html index.htm;
            client_max_body_size  100m;
         }


          location ~* "\.(htm|gif|jpg|png|js|css)" {//请求包含css等等 代理到html位置
            root /html/test2;
            index index.html;
            client_max_body_size  100m;
          }
		//这里规则也是包含,多个是或的关系,代理到1001(说明一下1001服务是一个后台分发网关)
          location ~* "(wms|login|api|export|import|upload|template|route)"  {
            proxy_pass http://127.0.0.1:1001;
            index  index.html index.htm;
            client_max_body_size  100m;
          }
         location / {//转发所有地址
            root /html/test2;
            index index.html;
            client_max_body_size  100m;
          }
        }
  }
alias与root区别

alias: 使用场景为从子目录查找

          location /css {
            alias   /home/java/public/css;
            index  index.html index.htm;
        }
#        location /css {
#            root   /home/java/public;
#            index  index.html index.htm;
#        }

http://xxx.com/css/loader.css 访问成功

  location /css {
    alias   /home/java/dc/dc_frontend/public;
    index  index.html index.htm;
}

访问404

location proxy_pass 截取问题
 location /micro/ {
	proxy_pass_header Server;
	proxy_set_header Host $http_host;
	proxy_redirect off;
	proxy_set_header X-Real-IP $remote_addr;
	proxy_set_header X-Scheme $scheme;
	proxy_pass http://192.168.0.241:8400/;  # 最后加 /
}

http://xxx.com/micro/stat 访问 -> http://192.168.0.241:8400/stat

cat websocket.conf

  map $http_upgrade $connection_upgrade { //map指令可以将变量组合成为新的变量
    default upgrade;
    ''      close;
  }

  server {
    listen       80;
    server_name localhost; //换成域名
    access_log /var/log/nginx/yourdomain.log;

    location / {
      proxy_pass http://192.168.0.4:2048;
      proxy_read_timeout 300s;//延长无数据传输断开时间

      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;//升级协议为WebSocket
      proxy_set_header Connection $connection_upgrade;
        }
    }


版权声明:本文为zoeou原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。