I have ngnix webserver running on port 80. I have a Nodejs running on 9000 port. I need to rewrite the request coming from my nginx to app server which is running on port 3000.
I tried configuring with below options but it is redirecting to the browser and not forwarding to the app server. When there is a request for /hosts.json it need to be redirected to http://appserver.corp.cn.com:3000/hosts/hosts.json
. Please let me know where I am going wrong.
server {
listen 80;
server_name localhost;
access_log /var/log/nginx/localhost.access.log;
location / {
rewrite ^/hosts.json http://appserver.corp.cn.com:3000/hosts.json permanent;
rewrite ^/hosts/bu.json http://appserver.corp.cn.com:3000/hosts/bu.json permanent;
proxy_pass http://localhost:9000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
I need to set proxy_pass
for the below url pattern
location ~ ^/hosts/bu/(.*)/app/(.*)$ {
proxy_pass http://appserver.cnma.com:3000/hosts/bu/$1/app/$2;
}
You want to proxy to different servers based on URI. There is a natural way for this, just out different proxy_pass
directives in appropriate location
s:
server {
...
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location / {
proxy_pass http://localhost:9000;
}
location /hosts.json {
proxy_pass http://appserver.corp.cn.com:3000;
}
location /hosts/bu.json {
proxy_pass http://appserver.corp.cn.com:3000;
}
location /hosts/bu/ {
proxy_pass http://appserver.corp.cn.com:3000;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.