Nginx: Add subpath cause infinite redirect loop - nginx

Hi I want to add a subpath to my current app url and redirect all request to it, example:
https://example.com/ -> https://example.com/main
https://example.com/faq -> https://example.com/main/faq
server {
listen 80 default;
server_name example.com;
rewrite ^/(?!main).*$ /main/$1;
}
I try to use regex to avoid the infinite loop on itself, but it still redirect, not sure what went wrong

You are missing a capture for $1 and you should make the rewrite issue a 3xx response if you want to see the result:
rewrite ^/(?!main)(.*)$ /main/$1 redirect;
See this document for more.

Related

Fourth-Level Subdomain Forwarding

I've recently been trying to set up a reverse proxy that would forward certain 4th-level subdomains to particular locations. So, for example, this is what I'm trying to accomplish (configuration in my nginx file):
server {
listen 80;
server_name *.server.domain.com;
rewrite ^ https://$server_name$request_uri;
}
The goal here being that if someone went to, for example, http://item1.server.domain.com, they would be re-routed to https://item1.server.domain.com. However, with this configuration, the URL gets rewritten to https://%2A.server.domain.com.
Is there a way to fix this so that the full domain (item1) gets added correctly to the rewritten URL? Ideally, I wanted it to eventually be able to rewrite any subdomain on server.domain.com directly to https.
Thanks!
The $server_name variable contains the text from the value of the server_name directive. The %2A is a URL encoded representation of the leading *.
Use $host or $http_host to obtain the hostname actually requested by the client. See this document for more.
For example:
server {
listen 80;
server_name *.server.domain.com;
return 301 https://$host$request_uri;
}
Note: Restart nginx and clear the browser cache between each test. Check the configuration using nginx -T.

Chage the part of the URL using nginx

I m using nginx webserver.
I want to change the url before it hits the server from
https://www.example.com/abc/contact-us
to
https://www.example.com/#/contact-us
Thanks in advance.
For a single URI redirection, an exact match location and return statement may be most efficient:
location = /abc/contact-us {
return 301 /#/contact-us;
}
To redirect all URIs beginning with /abc use a rewrite directive:
location ^~ /abc/ {
rewrite ^/abc(.*)$ /#$1 permanent;
}
The location block is largely redundant, but means nginx only looks at the regular expression when it needs to. See this document for more.

nginx redirect for a specific domain

I would like to redirect traffic from:
http://demo.foo.com/a.png
to http://demo.bar.com/a.png
One caveat is that foo.com and bar.com are both the same box. So I need specify that the redirect occur for foo.com traffic only, to avoid a redirect loop on bar.com traffic.
I tried this:
server {
server_name demo.foo.com;
return 301 $scheme://demo.bar.com$request_uri;
}
After waiting a minute for it to take effect... This looked promising:
curl -I http://demo.foo.com
The 301 appeared as expected. However, there are two issues:
1) running curl -I http://demo.bar.com also came back with a 301 redirect to demo.bar.com (so my server_name filter is not working)
2) after a few more minutes (~2-3) both curl commands stop working, and return a 503 Unavailable message (this may be due to a redirect loop caused by #1)
We are using such constraction:
server {
...
server_name demo.foo.com demo.bar.com;
if ($http_host = "demo.foo.com") {
rewrite ^ http://demo.bar.com:$server_port$request_uri permanent;
}
...
}

Nginx rewrite throws out 404 Not Found

After adding rewrite block, the Ubuntu 12.04 server hosting Rails 3.2.12 app throws out 404 Not Found error when entering mysite.com/nbhy.
Here nbhy is a symlink under root /var/www/ pointing to /var/www/nbhyop/current/public and it is for hosting rails app. The purpose of the rewrite is to rewrite to /nbhy/authentify/sigin when user entering /nbhy or /nbhy/
Here is server block in nginx.conf:
server {
listen 80;
server_name mysite.com;
root /var/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
}
location / {
rewrite "/nbhy" /nbhy/authentify/signin last;
rewrite "/nbhy/" /nbhy/authentify/signin last;
}
}
The error.log on nginx for the error is:
2013/06/09 21:36:31 [error] 32505#0: *1 open() "/var/www/nbhy/authentify/signin" failed (2: No such file or directory), client: 67.173.143.107, server: mysite.com, request: "GET /nbhy HTTP/1.1", host: "mysite.com"
Before adding rewrite location block, the system could bring up login page with url mysite.com/nbhy/authentify/signin. But now it throws out error after adding the rewrite block. What's wrong with the rewrite?
The location block must be within the server block:
server {
listen 80;
server_name mysite.com;
root /var/www/;
passenger_enabled on;
rails_env production;
passenger_base_uri /nbhy;
location / {
# Matches /nbhy /nbhy/ /nbhy////////////...
location ~ ^/nbhy/*$ {
return 301 /nbhy/authentify/signin;
}
# Other rules (regex) goes here
}
}
The symbolic link is from no concern at this stage of processing.
You are using rewrite, were the first pattern is always a regular expression. Mohammad AbuShady was absolutely right with his answer of using a more specific location. But using a rewrite is not necessary, a return is better because we want nginx to abort execution and simply—well—return.
Also note how I enclosed the regular expression within the generic block location / {} which matches all locations. This is the proper way to write location blocks according to Igor Sysoev. See this forum post for more info.
My configuration above is also catching URLs with more than one slash (hence the *) and is meant as a more user friendly matching because a user might simply type too many slashes. No need to punish them, instead answer with a proper redirect.
I hope this helps!
Related Links
How nginx processes a request
If I understand correctly, you have a URI your-host/nbhy which you want to rewrite to your-host/nbhy/authentify/signin, which should be linked to /var/www/nbhy/authentify/signin, which is symlinked to /var/www/nbhyop/current/public/authentify/signin?
It looks like nginx is complaining about not finding /var/www/nbhy/authentify/signin. Since you have a symlink from /var/www/nbhy to /var/www/nbhyop/current/public, there has to be a folder /var/www/nbhyop/current/public/authentify/signin. Are you sure there is one, and that your www-data user (or whatever user you're using) has rights to the directories above it?
Also, nginx has an option for disabling symlinks. Try setting that to off. That's the default, I know, but there might be another file that sets it to on.
It might also be that nginx doesn't follow the symlink because you added last to your rewrites. Try removing that, and see if it works then.
why not try adding a more specific location block
location ~ /nbhy/?$ {
rewrite ^ /nbhy/authentify/signin last;
}
or you could remove the regex but it would match longer urls like /nbhy/one/two
location /nbhy {
rewrite ^ /nbhy/authentify/signin last;
}

Nginx - Redirect Domain Trailing Dot

How can I redirect "http://domain.com." to "http://domain.com" with Nginx?
What's the recommended way of doing this? Regex or is there any other options?
The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.
if ($http_host ~ "\.$" ){
rewrite ^(.*) $scheme://$host$1 permanent;
}
This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.
You will need to use Regex.
server {
listen 80;
server_name domain.com.WHATEVER, domain.com.WHATEVER-2, domain.com.WHATEVER-3;
rewrite ^ $scheme://domain.com$request_uri? permanent;
}
From: http://wiki.nginx.org/HttpRewriteModule
redirect - returns temporary redirect with code 302; it is used if the substituting line begins with http://
permanent - returns permanent redirect with code 301

Resources