search

Home  >  Q&A  >  body text

How to write nginx reverse proxy configuration

The effect I want:

http://hostname/proxy/3000
http://127.0.0.1:3000

http://hostname/proxy/3000/anything
http://127.0.0.1:3000/anything

There is a requirement: the port is changed

I tried

location ~ /proxy/(\d+) {
        proxy_pass http://127.0.0.1:;
        rewrite ^/(.*)$ / break;
    }

But rewrite has problems no matter how it is written

How to write in nginx configuration, wait online~

滿天的星座滿天的星座2749 days ago463

reply all(1)I'll reply

  • 漂亮男人

    漂亮男人2017-05-16 17:28:53

    proxy_pass 的文档里有讲:location 使用了正则后,proxy_pass The URI part in the parameters following the directive will be ignored. You can use the following configuration to indirectly achieve the function you want:

    server {
        listen       80;
        server_name  localhost;
    
        location /proxy/ {
            rewrite ^/proxy/(\d+)/(.*)  /internal?port=&url= last;
        }
    
        location /internal {
            internal;
            proxy_pass http://127.0.0.1:$arg_port/$arg_url;
        }
    }   
    

    reply
    0
  • Cancelreply