I want to connect example.com:80 and example.com:82 to a specific site through reverse proxy.
localhost/api/buy/ -> http://example.com:80/
localhost/api/sell/ -> http://example.com:82/
I set up the config file as below.
server {
listen 80;
server_name localhost;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location = /api/buy {
return 302 /api/buy/;
}
location /api/buy/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://example.com:80/;
proxy_redirect off;
}
location = /api/sell {
return 302 /api/sell/;
}
location /api/sell/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://example.com:82/;
proxy_redirect off;
}
}
But If I connect /api/buy and /api/sell, I get a 404 not found error.
Below is the contents of the error.log file.
connect() failed (10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET /api/ HTTP/1.1", upstream: "http://example.com/api/buy/", host: "localhost"
When using a different port, I confirmed that the setting works well.
I want to set it to the same port, but is there any way?
답변1
best way is changed proxy_pass http://example.com:80/ to proxy_pass http://example.com:80/api/buy/
location = /api/buy {
return 302 /api/buy/;
}
location /api/buy/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header HOST $host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://example.com:80/api/buy/;
proxy_redirect off;
}