need
nginx server proxys front-end projects and anti-generation back-end servers. There is no problem with using it during development. After deployment, there is also a request for the root address. Enter nginx's address localhost:3000 to access the front-end address, but an error will occur when calling the back-end interface. It seems that I don't have enough knowledge of nginx. I found that when calling the back-end interface, there will be more /api/ in each request, but the back-end interface path does not have more /api/. Finally, the problem is solved by rewriting the url.
General reverse proxy
Generally, a unified prefix is defined, such as: api, and the configuration is as follows
server { listen 80; server_name default; location /api/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass ; } }
Then when requesting http:localhost/api/findOne, it will be forwarded to /api/findOne.
Just set proxy_pass. The request will only replace the domain name, and will not replace /api/ as well.
I now want to access http:localhost/api/findOne to forward to /findOne. If /api/ is removed, you can follow the following two configurations.
Solution
Plan 1Use rewrite and notice that there is no / at the end of proxy_pass. Rewrite rewrites url, and the final request is /findOne
server { listen 80; server_name default; location /api/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; rewrite ^/api/(.*)$ /$1 break; proxy_pass ; } }
Plan 2Add / after proxy_pass/apiThe following content is spliced into proxy_pass.
server { listen 80; server_name default; location /api/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass /; } }
This is the end of this article about the implementation plan of Nginx reverse proxy rewriting URL. For more information about Nginx reverse proxy rewriting URL, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!