目前我正在使用nginx服务器和使用nodejs服务器作为反向代理。我想让nginx服务器proxy_pass不同的服务器的位置。
假设我的域是subdomain.domain.com
当加载subdomain.domain.com/
时,它应该提供静态文件。
加载subdomain.domain.com/example1/req1/req2
时,应该路由到127.0.0.1:3000/req1/req2
。
加载subdomain.domain.com/example2/req1/req2
时,应该路由到127.0.0.1:8080/req1/req2
。
但是我的配置路由subdomain.domain.com/example1/req1/req2
到127.0.0.1:3000/example1/req1/req2
导致错误。(nodejs返回Cannot GET/example1)
我应该如何正确地写这个nginx配置文件?
尝试在location块中使用重写指令。
类似:
location /example1/ {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://xxxxxx;
}
您可以在http://nginx.org/en/docs/http/ngx_http_rewrite_module.html查看与重写模块相关的文档
你需要添加下面的代码片段到你的nginx.conf.
server subdomain.domain.com;
location / {
rewrite ^/example1/(.*)$ /$1 break;
proxy_pass http://127.0.0.1:3000;
}
}