[英]Nginx Location and URI matching
I am trying to run use nginx as reverse proxy, but having problems with URI matching 我正在尝试使用nginx作为反向代理,但是URI匹配存在问题
Application_1 running on 8080 Application_2 running on 8181 Application_1运行在8080上Application_2运行在8181上
/api/v1/sql url be directed to Application_1 / api / v1 / sql网址定向到Application_1
/api/v1/map and /tiles url be directed to Application_2 / api / v1 / map和/ tiles网址被定向到Application_2
and the rest of the url to the rails application. 其余的URL到rails应用程序。 Below is my attempt of the nginx config, but i think there is an error with Application_2 matching.
以下是我对Nginx配置的尝试,但我认为Application_2匹配存在错误。
upstream application_1{
server 127.0.0.1:8080;
}
upstream application_2{
server 127.0.0.1:8181;
}
location ^~ /api/v1/sql/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://application_1;
break;
error_page 404 = /;
}
location ^~ (/api/v1/map/ | /tiles) {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
proxy_pass http://application_2;
break;
error_page 404 = /;
}
location / {
if ($http_origin) {
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods "GET,POST,OPTIONS,PUT";
add_header Access-Control-Allow-Headers Content-Type;
add_header Access-Control-Max-Age 86400;
}
passenger_spawn_method direct;
root /var/www/html ;
passenger_enabled on;
rails_env development;
}
When i try to post like http://mywebsite.com/api/v1/map?q=abcd i get status message "301 Moved Permanently" but when i access from the application directly http://mywebsite.com:8181/api/v1/map?q=abcd . 当我尝试发布类似http://mywebsite.com/api/v1/map?q=abcd的消息时,我收到状态消息“ 301永久移动”,但是当我直接从应用程序访问时http://mywebsite.com:8181/ api / v1 / map?q = abcd 。 it works ok.
可以。
Also, http://mywebsite.com/api/v1/maps has to be served by rails instead of application_2 另外, http://mywebsite.com/api/v1/maps必须由rails而不是application_2提供
Can anyone help me with correct nginx locatin configuration? 有人可以帮助我正确配置nginx locatin吗?
Thanks 谢谢
I think your problem is related to having /
at the end of location definition. 我认为您的问题与在位置定义末尾加上
/
有关。
You are requesting /api/v1/map?q=abcd
and in your config you have /api/v1/map**/**
(with the trailing /
) Nginx tries to 301 redirect your /api/v1/map
request to /api/v1/map**/**
(because it has it configured with /
at the end) , right? 您正在请求
/api/v1/map?q=abcd
并且在您的配置中具有/api/v1/map**/**
(带有/
)Nginx尝试将301 /api/v1/map
请求重定向到/api/v1/map**/**
(因为它的末尾配置了/
),对吗?
I would recommned configuring your locations: 我建议您配置位置:
location ^~ /api/v1/sql
location ^~ /api/v1/map
^~
in nginx means match locations starting with and has a high priority. nginx中的
^~
表示匹配位置以开头并具有较高的优先级。 This way even your /api/v1/maps
uri should be matching OK. 这样,即使您的
/api/v1/maps
uri也应该匹配OK。
Hope it helps. 希望能帮助到你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.