I have my staging config setup like so:
server {
listen 80;
server_name staging.domain.com;
root /var/www/staging/public;
and my production config setup like this:
server {
listen 80;
server_name www.domain.com;
root /var/www/production/public;
With no other redirects or anything.
The issue is that even if I disable the production config I can still access the staging server at www.domain.com.
Why is it not being restricted to its configured subdomain?
I've answered a similar question like this before
Let me start with a small explanation on how nginx matches the hosts, quoting from how nginx processes a request
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.
When you disable the main server you only have 1 left, so nginx passes the request to it, if you want to avoid that you need to add a main server to block all unconfigured domains
server {
listen 80 default_server;
return 403;
}
Then run
sudo service nginx reload
Then you're set
Related
I spot an issue when trying to configure the server. On the server side I am using Nginx.
The issue is that I got 1 domain and 2 subdomains like:
www.test.com
www.api.test.com
www.panel.test.com
And these 3 urls are redirected to server with IP by Record A.
All i want to do is recognize from what url the user comes and return:
if user comes from www.test.com - show static html
if user comes from api - redirect to nodejs server which will serve data
if user comes from panel - redirect to nodejs server which will serve html
I don't know how to recognize from what url the user comes.
Is it possible to do that? If it is - just if you got some tutorials - i would be grateful! Unfortunately, I checked lots of tutorials, but I wasn't able to manage it.
Just as a information, I'm really bad at that piece (servers and configuration) - but want to learn it :)
Thank you!
The server_name directive assigns rules to server blocks for specific domain/host names:
server {
server_name test.com www.test.com;
listen 80; # IPv4
listen [::]:80; # IPv6
root /var/www/static-files;
}
server {
server_name api.test.com;
...
}
server {
server_name panel.test.com;
...
}
Alternatively, the $host variable could be used within server blocks that serve multiple hosts:
server {
server_name test.com www.test.com
api.test.com
panel.test.com;
return 200 "This hostname is $host";
...
}
I am a new user of nginx and I am following a video guide from Linode on youtube (How to Set Up an NGINX Reverse Proxy).
I have a working nginx and apache server both on port 80. I know that because when I type the ip address of both in firefox, it directs me to nginx/apache welcome page.
The youtube video configuration template is as follow (where the server_name is the linode ip) :
server {
listen 80;
listen [..]:80;
server_name 172.105.104.226;
location / {
proxy_pass http://localhost:3000/;
}
On my Proxmox machine, the nginx server is on a VM at 192.168.1.241 and the apache server on another VM at 192.168.1.243.
Looking at nginx documentation we find that this :
location /some/path/ {
proxy_pass http://www.example.com/link/;
}
should proxy all the traffic received on the nginx listening port and redirect it to the address specified by proxy pass.
With all these information, my configuration file is like this :
server {
listen 80;
listen [::]:80;
server_name 192.168.1.241;
location / {
proxy_pass http://192.168.1.243;
}
}
My understanding is that this configuration file should listen at the address 192.168.1.241 on port 80 (nginx server) and redirect it to the specified address 192.168.1.243 (apache server)/
If i understand correctly, Location / should take the request as is received on the nginx server and redirect it to the apache server.
However, when I enter 192.168.1.241 in my browser, it doesn't show the apache welcome message but shows the nginx welcome message. That means that the proxy isn't working.
My nginx understanding is extremely limited as I am just starting to learn, but to me it seems like this should work but doesn't.
Thank you for your help
It turns out that the configuration is correct.
The problem was that the webpage was cached. By forcing a full refresh, 192.168.1.241 redirected to 192.168.1.243 successfully.
My initial NGINX load balancer configuration was pretty simple:
upstream myapp {
server 10.11.12.13:80; #server01
server 10.11.12.14:80; #server02
}
server {
listen 80;
server_name localhost;
location /myapp/ {
proxy_pass http://myapp;
Let's say the localhost has the IP 1.2.3.4.
Result:
The user calls 1.2.3.4/myapp and gets redirected to one of those two servers including the requested filepath.
For example: 1.2.3.4/myapp/results gets redirected to maybe 10.11.12.13/myapp/results.
Now I have ONE special case to include, this is where I struggle. ALL requests should still be handled exactly the same with this one exception:
If 1.2.3.4/specialFilePath is called I want to redirect to a totally different, static URL e.g. externalPage.com.
Can I add this case somehow to my Nginx configuration?
You could add a second location block in which you defile what to do with the specialFilePath like
location /specialFilePath {
proxy_pass http://externalservice.com;
}
Then check the configuration with nginx -t or sudo nginx -t and reload the configuration
nginx server blocks are defined as
server {
listen 80;
We have a context where a series of server_name domains have their certificates issued on the server and wish to redirect them with
return 301 https://$server_name$request_uri;
however, a few other domains are not going to have a certificate issued. So the requirement is to listen to port 80 and redirect for a sub-set of domains while serving the others in plain http.
Should that be done with two nginx files, under Ubuntu 14.04, in /etc/nginx/sites-enabled/ or can this be syntactically done in a single file?
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;
}