Score:0

Nginx redirect all the incomming request base on the location block

us flag

I am using Nginx as a reverse proxy. I assume I have a domain like test.com and a location block with a path like /test. the location would proxy_pass to another website like http://test2.com. when I type the URL on the browser, it redirects the first request to the application (http://test.com/test/ --> http://test2.com/). However, All the incoming requests would be sent without the location which I did not define on Nginx. How can I make all the incoming requests follow that path? I want something like this:

  • first request: http://test.com/test/ --> http://test.com

  • incoming requests: http://test.com/test/statifile.js

  • http://test.com/test/api/something and so on...

    server {
     listen 80;
     charset utf-8;
     server_name test.com;
     location = /auth/test{
         internal;
         proxy_pass http://test.com/test/decisions/;
         proxy_pass_request_body off;
         proxy_set_header Content-Length '';
         proxy_set_header X-Original-URI $request_uri;
     }
     location = /test/decisions/ {
         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-Forwarded-Proto $scheme;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_set_header Authorization $http_authorization;
         proxy_pass http://valid.domain/test/api/authorization;
     }
     location /test/ {
         auth_request /auth/test;
         auth_request_set $auth_status $upstream_status;
         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-Forwarded-Proto $scheme;
         proxy_set_header Upgrade $http_upgrade;
         proxy_set_header Connection $http_connection;
         proxy_set_header Authorization $http_authorization;
         proxy_pass https://test2.com/;
         proxy_pass_header Server;
         proxy_http_version 1.1;
         proxy_redirect default;
         access_log /var/log/nginx/access.log;
         client_max_body_size 10240M;
     }
    }
    
Score:0
us flag

The term "incoming request" is confusing here. I assume you meant:

  1. Browser makes request to http://example.com/test/, which is proxied by nginx to http://2.example.com/.
  2. Browser makes subsequent request to http://2.example.com/statfile.js directly, when you want the requests to be made to http://example.com/test/statfile.js

With this problem statement, the optimal solution is to modify 2.example.com server configuration so that it creates URLs that point to example.com/test instead of URLs that point to 2.example.com. Most often there is a root URL setting in the web application that can be modified.

Less reliable and worse performance option is to use sub_filter module to replace all occurences of 2.example.com with example.com/test/ in the proxy_pass block.

However, this can have unintended side-effects.

Reza Kazemi avatar
us flag
I believe you are right. Thank you so much.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.