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区别
          location /css {
            alias   /home/java/dc/dc_frontend/public/css;
            index  index.html index.htm;
        }
#        location /css {
#            root   /home/java/dc/dc_frontend/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

你可能感兴趣的:(nginx)