2

Trying to let the url rewriting work in conjuction with the port forwarding but it seems doesn't work.

My application server is run at :8080 port while the nginx works at default 80 one. So I'm forced not only to rewrite the urls but also forward the request to another port.

Actually, the only I'm trying to do is query the existing resource http://localhost:8080/#/cat/tom by it's shortcut http://localhost/tom without any redirect.

Here's what I made based on nginx doc and other questions:

server { listen 80; server_name localhost; root /; #charset koi8-r; #access_log logs/host.access.log main; location / { rewrite ^/([a-z]+)$ /#/cat/$1 break; proxy_pass http://localhost:8080; proxy_redirect off; } ... } 

Have tried another configurations but neither does work. When I access the http://localhost/tom it says 404 not found while the http://localhost:8080/#/cat/tom works as expected.

Logs are haven't any error mention.

How to force this work as expected?

2 Answers 2

2

The problem is that #/cat/tom is an instruction to the client-side application and not the server-side application. Which means that it needs to be sent to the client (via a redirect).

For example:

location = /tom { return 302 /#/cat/tom; } location / { proxy_pass http://localhost:8080; ... } 

You mention in your question that you are trying to do this without a redirect, but unfortunately if you look at the structure of a URL, you will notice that anything after the # is not sent to the server. See this document for more.

2

nginx rewrites work only on the path part of the URL. It does not work on the query part, which is separated by ? nor does it work on the part separated by #, which is the fragment.

Actually, when entering your http://localhost:8080/#/cat/tom URL into your browser, your browser sends a request to get http://localhost:8080/, and your browser processes the part after #by itself.

So, you need to find out how exactly your application software works with these fragment parts, and make a configuration based on that.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.