Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
232 views
in Technique[技术] by (71.8m points)

Redirect application with fixed path with nginx

My server is behind a reverse proxy of our university. I got the domain myuniverse.org/mysubdomain/.

Unfortunately the application I want to use always changes the path: myuniverse.org/mysubdomain/ automatically becomes myuniverse.org/myappname/ and this domain is not accessible. If I access my server via IP from the university network everything is fine, because myIP/myappname works.

Is there a way to redirect this behavior with nginx?

server {
        listen 80;
        server_name myServerName;


        location / {
        proxy_pass http://10.11.0.3:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }

}
question from:https://stackoverflow.com/questions/65939430/redirect-application-with-fixed-path-with-nginx

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are trying to make your application think that it is on a path it is actually not. This can be done by rewriting destination at proxy_pass:

  location ~ ^/mysubdomain(/.*)?$ {
    proxy_pass http://10.11.0.3:8080/myappname$1$is_args$args;
    # this example does not include proxy_set_header for simplicity but you may need it
  }

However, I would not recommend you do that in most cases. The above will help you to get a response to the initial request, but because your application will think it is on a different path, consequent requests (to static files or sub-pages) may be broken if a link will contain /myappname in it. NGINX can rewrite URLs in proxied responses but it is another hack with its own downsides.

What you really need is to change the base URI of the application from /myappname to /mysubdomain. This will make it work with no hacks involved.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...