我的nginx.conf中有一条规则无效,我也不知道为什么。根据文档,它应该可以工作。部分配置如下所示。
端口8100上的第一条规则起作用并将呼叫http://example.com/api/domains重定向到https://localhost:8181/oan/resources/domains
# Working
server {
listen 8100 default_server;
server_name example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
root /var/www/html/example;
location /api {
proxy_pass https://localhost:8181/oan/resources; break;
}
# For ReactJS to handle routes
location / {
if (!-e $request_filename) {
rewrite ^(.*)$ / break;
}
}
}
# Not working
server {
listen 8200;
server_name api.example.com;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
location / {
proxy_pass https://localhost:8181/oan/resources; break;
}
}
对端口8200的最后一次调用:http://api.example.com:8200/domains应该重定向到:https://localhost:8181/oan/resources/domains,但不能这样做。
此配置有什么问题,如何在端口8200上获取最后一条规则,可以正确执行操作,请始终重定向到https://localhost:8181/oan/resources/ $ uri
[当在prefix位置块中将proxy_pass
与optional URI一起使用时,Nginx将通过执行纯文本替换来转换请求的URI。
在您的情况下,前缀位置值为/
,可选URI值为/oan/resources
。因此,请求的/foo
URI将转换为/oan/resourcesfoo
。
为了正确操作,两个值都应以/
结束,或者都不以/
结束。
例如:
location / {
proxy_pass https://localhost:8181/oan/resources/;
}
有关详细信息,请参见this document。