Welcome! Log In Create A New Profile

Advanced

How to get nginx to redirect to another path only if the root path is requested?

Posted by leeand00 
How to get nginx to redirect to another path only if the root path is
requested?

Here is part of my server configuration:

server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

# Make site accessible from http://localhost/
server_name wiki wiki.leerdomain.lan;

# Note: There should never be more than one root in a
# virutal host
# Also there should never be a root in the location.
#root /var/www/nginx/;

rewrite ^/$ /rootWiki/ redirect;


location ^~ /rootWiki/ {
resolver 127.0.0.1 valid=300s;
access_log ./logs/RootWiki_access.log;
error_log ./logs/RootWiki_error.log;
proxy_buffers 16 4k;
proxy_buffer_size 2k;
proxy_set_header Host $host;
proxy_set_header X-Real_IP $remote_addr;
rewrite /rootWiki/(.*) /$1 break;
proxy_pass http://192.168.1.200:8080;
}

location ^~ /usmle/ {
access_log ./logs/usmle_access.log;

...

When I configure it as above I am unable to access any of the sub-locations
under root...but the root directory does forward to /rootWiki/ but I receive
a 502 Bad Gateway instead of the application on port 8080.

When I remove the line:

rewrite ^/$ /rootWiki/ redirect;

I'm able to access the rootWiki application, and all the sub locations from
root just fine.

It seems to me like it should work but it does not appear to.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278283,278283#msg-278283

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
On Fri, Jan 26, 2018 at 02:24:16PM -0500, leeand00 wrote:

Hi there,

> How to get nginx to redirect to another path only if the root path is
> requested?

I don't understand the rest of your mail; perhaps if you could show one
request that you make and the response that you get, and how it is not
the same as the response that you want, that would be clearer.

But this first question, I do understand.

location = / { return 301 /rootWiki/; }

should do what you want.

Good luck with it,

f
--
Francis Daly francis@daoine.org
_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
I have other subfolders in my location paths so for instance other than just
/ after my host, I also have /lang/english/grammar, and
/lang/spanish/gramática

But I figured it out:

location = / {
resolver 127.0.0.1 valid=300s;
proxy_pass http://192.168.1.200:8080/;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
rewrite /(.*) /$1 break;
access_log ./logs/root_access.log;
error_log ./logs/root_error.log;
}

The = / that you suggested fixed it right up, thanks!

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,278283,278289#msg-278289

_______________________________________________
nginx mailing list
nginx@nginx.org
http://mailman.nginx.org/mailman/listinfo/nginx
Sorry, only registered users may post in this forum.

Click here to login