最近项目遇到根据nginx根据目录进行转发,经过查阅资料及不断尝试终于完成,特此自己记录下,给有需要的朋友参考。

        浏览器请求路径为:http://localhost/{passid}/{cid}/api/oa/index.html,其中passid及cid是可变项用于前端通过url获取作为参数传给后端。api为固定项,用于nginx做及目录转发。实际访问uri为:http://localhost/oa/index.html。所以nginx转发需求为:http://localhost/{passid}/{cid}/api/oa/index.html -> http://localhost/oa/index.html

server {
        listen       80;
        server_name  localhost;

        location ~ /api/ {
		
			proxy_pass http://127.0.0.1:9996;
            #root html;
            #index  index.html index.htm;
			#rewrite_log on;
			
			rewrite "^/(.*)/api\/(.*)$" /$2 break;
        }

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

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐