Nginx conflicting server name race condition - nginx

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

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.

rewrite is not working as desired for redirect URL in NGINX

I have an Nginx problem and after reading a lot and trying multiple combinations, I do not find the solutions to my problem.
I have a Jira server behind a reverse proxy using Nginx. My problem is that I want to have jira.mydomain.com as the default url but when using support.mydomain.com I want it to redirect to https://jira.mydomain.com/servicedesk/customer/portal/1 because is where the Service Desk. This is what I do not make.
The result that I have now is that both jira.mydomain.com and support.mydomain.com go to the same place which probably is the normal behavior and I do not know how to configure it but if anyone can help would be very appreciated.
Thanks to all of you and the hivemind.
This is what I have right now
/etc/nginx/conf.d/default.conf
upstream jira {
server jira_ip_adress:8081;
}
server {
server_name jira.mydomain.com;
server_name support.mydomain.com;
rewrite ^/support.mydomain.com https://jira.mydomain.com/servicedesk/customer/portal/1;
[...]
location / {
proxy_pass http://jira;
[...]
}
Nginx uses the server_name directive to match the domain name part of a URL. You need to use two server blocks, one for each of the domain names.
For example:
server {
server_name support.mydomain.com;
return 301 https://jira.mydomain.com/servicedesk/customer/portal/1;
}
server {
server_name jira.mydomain.com;
...
}
Obviously, if these are https services, you will need to add the appropriate listen statements to both server blocks.
Use nginx -T (uppercase T) to test the configuration file and view the entire configuration across all included files.
Many many thanks. I tried this yesterday but I had the next error when using nginx -T
nginx: [emerg] unexpected "E" in /etc/nginx/conf.d/default.conf:183
nginx: configuration file /etc/nginx/nginx.conf test failed
I have tried again after your help, and you spotted me in the right direction because today have been able to know what the error was telling to me and fixed.
So know, with your suggestion, is working like a charm.
Thanks a lot!

Nginx server names priority

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

Nginx how to pass a parameter to proxy server?

I have a very simple problem but I am new to nginx so I may be missing some obvious solution.
So I have a location defined in nginx
/example/$id
I would like to pass $id to parametrized route on my proxy server like so
http://server.com/example/$id
This gives me unknown id variable on nginx reload. So my question is how can I pass a parameter from nginx to my proxy server.
Assuming all the rest are correctly set, you can just create a location with a regex and pass the captured variable to your proxy.
location ~ ^/example/(.+)$ {
proxy_pass http://server.com/example/$1;
}
If your $id is numeric only, the regex could be more restrictive
location ~ ^/example/(\d+)$ {
proxy_pass http://server.com/example/$1;
}
Note that you can't just use a variable without declaring it first. Declaring $id is not necessary, it is captured inside the parentheses of the regex and passed to $1

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

Resources