Search code examples
nginxreverse-proxy

nginx reverse proxy with subpage as parameter


I'll try to handle all requests at my server URL's in nginx to another service at my server. The service shouldn't be visible from outside, so I will use the reverse proxy functionality of nginx. So the requests to nginx should be for example:

Currently I don't need get parameters, but of course it would be nice if they'd work too, i.e.:

Currently my config is like that:

...
    location / {
            gzip off;

            proxy_read_timeout 300;
            proxy_connect_timeout 300;
            proxy_redirect off;

            proxy_http_version 1.1;

            proxy_set_header Upgrade $http_upgrade;

            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Ssl on;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header X-Forwarded-Proto $scheme;
            proxy_set_header X-Frame-Options SAMEORIGIN;
            proxy_pass http://localhost:1234/;
            proxy_set_header Proxy "";
...

What's of course don't redirect the subpages. But following wasn't working too:

...

    location /(.*) {
    # location ~ /(.*) { # I tried this too

    ...
     
           proxy_pass http://localhost:1234/?_=$1;
...

Can anyone please help me?


Solution

  • You can change the URI before it gets processed by the proxy_pass statement, by adding a rewrite...break; statement inside the location / block. See this document for details.

    For example:

    location / {
        ...
        rewrite ^(/.*)$ /?_=$1 break;
        proxy_pass http://localhost:1234;
    }