nginx - nginx位置和proxy_pass的问题
问题描述
我的 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
解决方案
当您在前缀proxy_pass
位置块中使用可选 URI时,Nginx 将通过执行直接文本替换来转换请求的 URI。
在您的情况下,前缀位置值为/
,可选的 URI 值为/oan/resources
. 所以请求的 URI/foo
将被转换为/oan/resourcesfoo
.
为了正确操作,两个值都应该以 结尾,/
或者都不以 结尾/
。
例如:
location / {
proxy_pass https://localhost:8181/oan/resources/;
}
有关详细信息,请参阅此文档。
推荐阅读
- spring-boot - 如何在spring boot中使用websocket并响应js?
- node.js - 在 Heroku 发布 bash 脚本中使用环境变量
- css - 当前位置:Sticky 无法在 CSS Squarespace 中工作
- r - 创建一个通过所有 id 运行的函数并将该函数应用于它们
- google-sheets - 我想根据不同的关键词显示一个单词列表
- jquery-ui - Jquery UI 最初隐藏滑块句柄
- python - Python - 将字符串匹配到csv值,然后提取相邻列
- nvidia - Direct3D11 可以使用体积平铺纹理吗?
- python - 通过函数清除 tkinter 嵌入式图形
- jenkins - 为 webhook 触发器使用 Jenkins Job-DSL 配置块