Nginx server names priority - nginx

I have two server sections for nginx in different files.
The first one:
server {
server_name _;
...
}
The second one:
server {
server_name ~someRegex;
...
}
I have some constraints - I can't change the first server section (i.e. I can't edit first file)
Documentation says the following about server names priority:
exact name
longest wildcard name starting with an asterisk, e.g. “*.example.org”
longest wildcard name ending with an asterisk, e.g. “mail.*”
first matching regular expression (in order of appearance in a configuration file)
As I understand server_name _ is used as catch-all server.
So when I have request from host matched someRegex request is handled by first server section. Is there a way to handle these request by second server section?

Not quite.
_ simply renders the server_name invalid. See this document.
What makes a server block the default is either being defined first for a given port or being defined with the listen ... default_server modifier. See this document.
So your configuration will work as you expect, assuming that your regex is valid and that the second server block has indeed been installed by nginx. Check your error log after reloading nginx and/or test the configuration using
nginx -t

Related

Nginx possibly removing dot (".") from URL path before forwardslash

I have defined a reverse proxy like this:
server {
listen 443 ssl;
server_name testing.com;
ssl_certificate "C:/nginx/testing.crt";
ssl_certificate_key "C:/nginx/testing.key";
location / {
proxy_pass "http://127.0.0.1:8888/";
}
}
The reverse proxy works as intended. Now that we got that out of the way:
I have a case where i need to pass parameters in the URL and some of the parameters sometimes end with a dot (.) like this "https://testing.com/param1./param2/param3/param4."
But for some reason the URL that is received at the server looks like this "127.0.0.1:8888/param1/param2/param3/param4"
If i call the server directly like this "127.0.0.1:8888/param1./param2/param3/param4.", the parameters are correct. My guess is that nginx modifies the URL. Maybe the issues lies somewhere else...
I am on Windows 10. The server is a Go (golang) server that uses only built in libraries. I have setup self signed certificates and edited my hosts file (never had issues with those).
ALSO - my friend who is also working on this project has no issues even tho we have identical nginx setups, but the only difference is that he is on Linux.

Nginx conflicting server name race condition

In the same nginx.conf I have a server block for:
server {
server_name *.mysite.com
...
}
and:
server {
server_name subdomain.mysite.com
...
}
Will there be a conflict? Or the more specific server_name will win the race condition? Is there any significance to the order they appear in the config?
No race condition or nothing of that sort will happen.
There is precedence defined for this.
order of precedence:
exact name
longest wildcard name starting with an asterisk, e.g. “*.example.org”
longest wildcard name ending with an asterisk, e.g. “mail.*”
first matching regular expression (in order of appearance in a configuration file)
More on how NGNIX processes requests: http://nginx.org/en/docs/http/server_names.html

nginx- duplicate default server error

In my error log i get
[emerg] 10619#0: a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/mysite.com:4
on Line 4 I have:
server_name mysite.com www.mysite.com;
Any suggestions?
You likely have other files (such as the default configuration) located in /etc/nginx/sites-enabled that needs to be removed.
This issue is caused by a repeat of the default_server parameter supplied to one or more listen directives in your files. You'll likely find this conflicting directive reads something similar to:
listen 80 default_server;
As the nginx core module documentation for listen states:
The default_server parameter, if present, will cause the server to become the default server for the specified address:port pair. If none of the directives have the default_server parameter then the first server with the address:port pair will be the default server for this pair.
This means that there must be another file or server block defined in your configuration with default_server set for port 80. nginx is encountering that first before your mysite.com file so try removing or adjusting that other configuration.
If you are struggling to find where these directives and parameters are set, try a search like so:
grep -R default_server /etc/nginx
OS Debian 10 + nginx.
In my case, i unlinked the "default" page as:
cd/etc/nginx/sites-enabled
unlink default
service nginx restart
Execute this at the terminal to see conflicting configurations listening to the same port:
grep -R default_server /etc/nginx
If you're on Digital Ocean this means you need to go to /etc/nginx/sites-enabled/ and then REMOVE using rm -R digitalocean and default
It fixed it for me!
Pic of Console on Windows 10 using Bitvise
In my case, commenting out the wildcard directive on include in the /etc/nginx/nginx.conf worked
#include /etc/nginx/sites-enabled/*;
include /etc/nginx/sites-enabled/abcdef.com;
PS: as per the comments above, this could be a solution if there is just one configuration (either default or your custom one)
In my case junk files from editor caused the problem.
I had a config as below:
#...
http {
# ...
include ../sites/*;
}
In the ../sites directory initially I had a default.config file.
However, by mistake I saved duplicate files as default.config.save and default.config.save.1.
Removing them resolved the issue.
If davidjb's answer does not show multiple default_server lines, check for multiple include directives.
It is possible you accidentally included your default (or another site) twice.

Default nginx conf file

Suppose I have 3 nginx conf files and default_server is not defined in any of them. Now if a request comes to the serve and If its value does not match any server name, or the request does not contain that header field, It will take which nginx config to serve the request.
I mean how is it prioritized?
I guess, first vhost would be used. If you use including virtual hosts (/etc/nginx/sites-enabled/*) the hosts would be included in alphabetical order. So, if you have hosts "a", "b" and "c", first of them will be "a".
Please take a look at nginx documentation Server names
If no server_name is defined in a server block then nginx uses the
empty name as the server name.
nginx versions up to 0.8.48 used the machine’s hostname as the
server name in this case

Why does nginx return a Error 324 with certain query strings?

I have two servers, with identical minimal configuration (as far as I know!).
On server A, a query for
http://xxx.yyy.zzz.A/
returns the default nginx index.html page
On server B, a query for
http://xxx.yyy.zzz.B/
returns the default nginx index.html page
On server A, a query for
http://xxx.yyy.zzz.A/?%2F
returns the default nginx index.html page
On server B, a query for
http://xxx.yyy.zzz.B/?%2F
returns Error 324 (net::ERR_EMPTY_RESPONSE)
%2F is a CGI encoded forward slash, which is how I found this problem. It also seems to happen on %2G, %2H and %2I. I stopped testing here.
The 324 request does not show in access or error logs.
The relevant nginx.conf is
server {
listen 80 default_server;
server_name "";
location / {
root html;
index index.html index.htm;
}
}
What could possibly be the issue, or how could I further track it down?
Often problems like "hey this works half the time" or "this works on server A but not on server B" are loadbalancing/ proxy problems.
Did you check the configuration of your loadbalancer? Big chance something is wrong there and the errors are logged there.

Resources