Nginx server matching too many URLs - nginx

I've got a domain example.com and an "alternative" of some-example.com. I'm trying to direct traffic from some-example.com to example.com using a simple server declaration in nginx as follows:
server {
listen 80;
server_name some-example.com;
rewrite ^/(.*) http://example.com/$1 permanent;
}
I'm not 100% sure if this is the right rule, but I've got another vhost on the server, this isn't where the problem is, but it's necessary to understand the issue I'm having.
server {
listen 8745;
server_name localhost;
<other stuff goes here>
}
Hitting <my server IP>:8745 will go to that vhost, that works as intended. However I've got another vhost like this:
server {
listen 8746;
server_name localhost;
<other stuff goes here>
}
But all of my requests to <my server IP>:8746 hit example.com. I'm... baffled, I don't really grok nginx, so any help would be appreciated as to why this is happening. I mentioned that rule in the beginning because I'm thinking it has something to do with this. If additional information is needed I can supply it.
(Also, would this be better for Server Fault?)

I asked this on Server Fault also, however I found out the cause on my own. The below excerpt is pulled from this question.
It turns out this isn't an nginx
issue. I probably should've noted that
<my server IP>:8746 runs a Wordpress
installation; Wordpress has an option
set (in the wp_options table, the row
has option_id of 2 for me and
option_name of siteurl) to <some
domain>.com, I changed that to <some
domain>.com:8746 and it worked fine.

Related

DDEV: Redirect http to https using nginx-fpm and on various domains

I'm moving some small websites in production to DDEV and, some of them has multiple domains with a 301 redirection to the main HTTPS site.
This config was working well with the "natural" Nginx when I was using a .conf file to manage the domains that should be redirect to the main site on this way:
server {
listen 80;
server_name .domain1.com
.domain2.com
.domain3.com
;
return 301 https://www.maindomain.com;
}
I tried to create a new domains.conf file and add it inside the .ddev/nginx_full directory to be loaded in the restart process but seems the Nginx didn't recognize such file.
In the main "natural" Nginx config file I has this server to redirect all requests coming from HTTP to HTTPS:
server {
listen 80;
access_log off;
error_log off;
server_name maindomain.com www.maindomain.com;
return 301 https://www.$host$request_uri;
}
I tried to add these configs inside the .ddev/nginx_full/nginx-site.conf file but the server start to be crazy, doing sometimes infinite redirections and sometimes, not recognize the domains.
Inside the config.yaml file I have:
additional_fqdns:
- domain1.com
- domain2.com
- domain3.com
- maindomain.com
- www.maindomain.com
use_dns_when_possible: false
I'm sure that's a "right way" to handle this situation but, looking the docs, I didn't find and answer for that. On this way, I ask if someone here have the catch for that.
Thanks a lot
I think this will work for you.
Add the file .ddev/nginx/redirect.conf with these contents:
if ($http_x_forwarded_proto = "http") {
return 301 https://$host$request_uri;
}
This uses a DDEV nginx snippet, it could also be done with a full nginx config.
The ddev-router acts as a reverse proxy that terminates SSL/443 and passes along requests on port 80 to the web container.
You see the infinite redirects because it sees the request always on port 80.

Should I use Nginx or Route53 for redirection?

I have problem that I don`t know how to fix. I want when someone go to webmail.mywebsite.com get redirect to gmail.com
So far I try to do it using Nginx using this code
server { Server_name webmail.mywebsite.com return 301 https://www.gmail.com; }
But nothing happens I get 'DNS_PROBE_FINISHED_NXDOMAIN' error on my browser.
Can anyone help me how to solve this problem.
Also I want to mention that I have SSL (let`s encrypt) install on my ec2
can you try following code block for NGINX? Looks like you are missing http/https port number.
Once configured please validate the NGINX syntax and restart NGINX server.
server {
# For HTTP
listen 80;
# For HTTPS
listen 443;
server_name webmail.mywebsite.com;
return 301 https://www.gmail.com;
}

Nginx rewrite disabled? Can't do a simple rewrite

I've been having a lot of trouble with rewrite directives - nothing seemed to actually work, so I tried making the most basic rewrite sample that I could:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /var/www/html;
location / {
rewrite ^/a$ /test.html break;
}
}
I was assuming this would return the contents of /var/www/html/test.html at http://localhost/a but it doesn't, it gives me a 404. Permissions on test.html are set correctly and it is accessible at http://localhost/test.html. I'm assuming rewriting is actually disabled - but I can't find anything like that in nginx.conf. The settings file above is the default in sites-available, and other than adding rewrite_log on; to the nginx.conf file everything is stock as distributed by the nginx 1.12.1 package in Ubuntu (17.10).
Logs provide no useful information and enabling rewrite logging has added no extra information to the logs. And yes, I've restarted nginx after each edit of the settings.
I've been trying all sorts of combinations, so at this point I'm just assuming rewrites aren't working/enabled? I'm totally confused here - ANY hints would be a big help.
I have no idea why but changing break to last made it work. This is confusing to me, as with this configuration I wouldn't expect there to be any processing after the rewrite. This is a pretty fresh install and there's not actually anything else on it yet that I would think would be interfering - so there must be some reason for this? If anyone has a better answer I'll switch the accepted, but for now this 'worked'.

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.

nginx with 2 domains pointing to same root

I'm a noob in nginx. I start configuring my domains (marianamarques.ntr.br and fabricadevozes.com.br) dns to point to my aws ec2 instance public ip. Thats ok.
When i start configure the nginx:
i created /var/www
i created /var/www/marianamarques.ntr.br/public_html
i created /var/www/fabricadevozes.com.br/public_html
i created /etc/nginx/sites-availble/marianamarques.ntr.br.conf listening to port 80 with root pointing to /var/www/marianamarques.ntr.br/public_html
i created /etc/nginx/sites-availble/fabricadevozes.com.br.conf listening to port 80 with root pointing to /var/www/fabricadevozes.com.br/public_html
When i tell on browser http://www.fabricadevozes.com.br i got htmls from /var/www/fabricadevozes.com.br/public_html but when i tell on browser http://www.marianamarques.ntr.br i got htmls from /var/www/fabricadevozes.com.br/public_html too.
I'm a bit desperated. My nginx was installed from apt-get and after hours and hours of web searches i know my /etc/nginx/conf.d/nginx.conf was missing (i don't have this file) but my nginx server starts with no issues.
Anybody can help?
nginx.conf should be located in /etc/nginx.
Post it along with your config files in sites-enabled and it'll be easier to tell you exactly what's wrong, but sounds like you may have a mistake in the server_name or root directives in your server definition. Make sure you specify the server name with and without the www. It could be loading your default domain if you didn't specify www. in the server name
server {
listen 80;
server_name marianamarques.ntr.br www.marianamarques.ntr.br;
root /var/www/marianamarques.ntr.br/public_html;
...
}
If thats not it, we'd need to see the config files.
Solved based on the link Nginx no-www to www and www to no-www - i create a second server listening to same port but expecting the server_name with-www prefix and then rewrite this to my other server point to without-www prefix like this:
server {
server_name www.domain.com;
rewrite ^(.*) http://domain.com$1 permanent;
}
server {
server_name domain.com;
#The rest of your configuration goes here#
}
I expect it can help others. Thanks a lot for everything! And sorry... my english is very poor!
Apparently you solved the problem but let me explain why it happened, you should read How nginx processes a request
Quote:
In this configuration nginx tests only the request’s header field
“Host” to determine which server the request should be routed to. If
its value does not match any server name, or the request does not
contain this header field at all, then nginx will route the request to
the default server for this port. In the configuration above, the
default server is the first one — which is nginx’s standard default
behaviour. It can also be set explicitly which server should be
default, with the default_server parameter in the listen directive
When you had a www or a non-www site missing, nginx couldn't match it to any server so it sends the request to the default server, and assuming you removed the default file from sites-enabled and didn't set any as default then the default server is the first one alphabetically, if we compare marianamarques.ntr.br.conf vs fabricadevozes.com.br.conf then the winner is the one starting with an f, that's why it was showing that server instead.
And since you did the basic redirection server, let me add that you better have used return over rewrite, check taxing rewrites
server {
server_name www.example.com;
return 301 http://example.com$request_uri$is_args$query_string;
}

Resources