How can I do (if-else) condition for subdomains?
first check >> abc.example.com
second check >> xyz.example.com
...
others (case else:) >> *.example.com (This part is important for me, I cannot do that)
server {
listen 80;
server_name abc.example.com;
...
}
server {
listen 80;
server_name xyz.example.com;
...
}
server {
listen 80;
server_name *.example.com;
...
}
Related
For example, I have a server with 2 network interface, one for public ip and one for private ip. And write 2 nginx configuration file:
cat /etc/nginx/sites-enabled/siteA.sample.edu.cn
server {
listen 80;
server_name siteA.sample.edu.cn;
...
location / {
root /var/lib/www/siteA.sample.edu.cn;
index index.html index.htm index.php;
}
}
cat /etc/nginx/sites-enabled/siteB.sample.edu.cn
server {
listen 80;
server_name siteB.sample.edu.cn;
...
location / {
root /var/lib/www/siteB.sample.edu.cn;
index index.html index.htm index.php;
}
}
As long as they both listen on 80 without ip restriction, they can work together well. Setting local dns for siteA and siteB with the same ip 172.16.0.1, I can visit different site with those url.
But when setting explict listen ip to one site:
cat /etc/nginx/sites-enabled/siteA.sample.edu.cn
server {
listen 172.16.0.1:80;
server_name siteA.sample.edu.cn;
...
}
}
cat /etc/nginx/sites-enabled/siteB.sample.edu.cn
server {
listen 80;
server_name siteB.sample.edu.cn;
...
}
}
Then I cannot visit siteB.sample.edu.cn anymore. Using url http://siteB.sample.edu.cn will finally reach the siteA.sample.edu.cn.
So how to stop such strange redirection? It seems that server with explicit listen ip has higher priority?
This behaviour is documented here.
You could try using two listen directives in site B's server block.
For example:
server {
listen 172.16.0.1:80;
listen 80;
...
}
Or:
server {
listen 172.16.0.1:80;
listen <otherIP>:80;
...
}
When I open example.org, I'd like my browser to translate it to www.example.org, how should I change my configuration to do so ?
server {
listen 80;
server_name example.org www.example.org;
...
}
You can just redirect:
server {
listen 80;
server_name example.org;
return 301 http://www.example.org;
}
server {
listen 80;
server_name www.example.org;
...
}
Nginx is 1.14.1 version
have several virtual hosts and default in the /etc/nging/sites-enabled:
I've tried to configure using this doc: http://nginx.org/en/docs/http/request_processing.html
default
server {
listen 80;
server_name "";
return 444;
}
server {
listen 443 ssl http2 default_server;
server_name _;
ssl_certificate ....
ssl_certificate_key .....
add_header Strict-Transport-Security 'max-age=31536000';
return 444;
}
domain1
server{
listen 80;
server_name domain1;
return 301 https://$server_name;
}
server {
server_name domain1;
listen 443 ssl;
..................
}
but when tried to get access using server IP nginx redirect to domain1. please help what's wrong here. I'd like to deny access by IP to sites and leave only requests with domain name
I have two configs enabled in my nginx sites-enabled folder.
The first one (my-domain.fr.conf) looks like this:
server {
listen 443 ssl http2;
server_name my-domain.fr;
index index.html;
location / {
root /www/my-domain.fr;
}
include ssl_certif.conf;
}
# HTTP redirect
server {
listen 80 default_server;
server_name my-domain.fr;
location / {
return 301 https://my-domain.fr$request_uri;
}
}
The second one (sub.my-domain.fr.conf) looks like this:
server {
location / {
proxy_pass http://127.0.0.1:8080;
}
include ssl_certif.conf;
server_name sub.my-domain.fr;
listen [::]:443 ssl;
}
server {
if ($host = sub.my-domain.fr) {
return 301 https://$host$request_uri;
}
server_name sub.my-domain.fr;
listen [::]:80;
return 404;
}
I would expect the last one to only catch requests to sub.my-domain.fr subdomains, but instead it catches anything (I have wildcards subdomains set up on my DNS), and even masks my-domain.fr.
How can I make sure it only catches sub.my-domain.fr requests?
I found the reason.
sub.my-domain.fr supports ipv6 (listen [::]:443 ssl;). my-domain.fr doesn't.
I suppose my connection is using ipv6 when it can, and in this case, sub.my-domain.fr is the only match.
Adding ipv6 support (listen 443 ssl => listen [::]:443 ssl;, and listen 80; => listen [::]:80;) in all server entries fix it.
hostname1 and hostname2 represent the same host running nginx. How request is passed when user visits hostname2:80 on another machine?
nginx.conf
server {
listen 80;
server_name localhost;
# other content
}
server {
listen 80;
server_name hostname1;
# other content
}