configuring multiple domains with nginx - nginx

First of all there's already a server running on 80 port(with domain, let's say domainA.com ), and I have another domain(domainB.com). Here's what I'm trying to do.
80 port: domainA.com
3000 port: domainB.com
so If I make a request to domainB.com It shouldn't redirect to domainA.com:3000 but should actually works on the same server. and DNS server ip is set to the server as the same as domainA.com is connected to (so if I go to domainB.com it just redirects to domainA.com, I think I need to fix this part but I have no idea). how can I make this happen?

server {
listen 80;
server_name domainA.com;
root /var/www/domainA;
}
server {
listen 3000;
server_name domainB.com ;
root /var/www/domainB;
}
you now can access this sites via : domainA.com and domainB.com:333
also you can make both of them listen on port 80 :
nginx can detect that request is coming for which domain and redirect request to that domain :
server {
listen 80;
server_name domainA.com;
root /var/www/domainA;
}
server {
listen 80;
server_name domainB.com ;
root /var/www/domainB;
}
see this : nginx server_names
source : diffrent domain on same ip

Related

How to make nginx process only the specified host (alocal, blocal), but ignore the "naked" ip address?

I have two rules for nginx (local):
server_name alocal;
listen 80;
location / {
...
proxy_pass http://localhost:8081;
}
and
server_name blocal;
listen 80;
location / {
...
proxy_pass http://localhost:8082;
}
I also changed the "hosts" file.
C:\Windows\System32\Drivers\etc\hosts
127.0.0.1 alocal
127.0.0.1 blocal
Everything works well. When I make a request through the browser, I get the expected behavior.
http://alocal -> http://127.0.0.1:8081
http://blocal -> http://127.0.0.1:8082
But when I specify just "localhost" as a host, nginx still processes my request, and it takes the first rule that comes along (from those that I gave above).
http://localhost -> http://127.0.0.1:8081
or (depends on which rule comes first)
http://localhost -> http://127.0.0.1:8082
Why does nginx process localhost if other hosts (alocal, blocal) are specified in server_name?
How to make nginx process only the specified host (alocal, blocal), but ignore the "naked" ip address?
Nginx listening to Port 80. If there is a request not matching any server_name the default is taken. Either given by listen 80 default_server; or the first entry if omitted.
If you want to block all requests not matching the specified server_name or with empty Host-Header you need a catch all as last block:
server {
listen 80 default_server;
server_name "";
return 444;
}
It will be not really ignored, but rejected. You can't "ignore" something, nginx is blocking port 80 and handling all requests somehow.

Nginx redirect from old subdomain to new subdomain

I want to redirect all traffic from old subdomain to new subdomain but something went wrong =)
Here is my config:
# Redirect from http to https
server {
listen 80 default deferred; # for Linux
return 301 https://$host$request_uri;
}
# Redirect section - from old to new sub
server {
listen 80;
listen 443 ssl http2;
server_name old.domain.com www.old.domain.com;
return 301 https://new.domain.com$request_uri;
# should I use SSL while redirecting? without old certs nginx gives me an error...
ssl_certificate ...
...
}
# main working subdomain
server {
listen 443 ssl http2;
server_name new.domain.com www.new.domain.com;
root ...
...
}
With this config I can access to new.domain.com and it is working as expected with valid SSL cert, but old old.domain.com is also accessible without redirection :(
My variant is working, it was an error on DNS provider side (something was wrong with DNS records, CNAME)

Nginx: how to do context path redirect,rewrite or reverse and proxy setting for application

I am new to NgInx.
I don't know i have to ask or not but step forward and i need it for my application redirection.
I have 2 Asp.net web applications and based on domain and context path i need to redirect to particular application.
app1
env.example.com:9060
app2
env.example.com:9040
for example
Step1: if i hit this given url in browser any one application should redirect
domain Url1: abc.example.com
domain url2: xyz.example.com
Step2 if i hit this given url in browser with context path then crossponding application should redirect
domain Url1: abc.example.com/app1==>redirect to app1
domain url2: xyz.example.com/app2==>redorect to app2
Looking for server listen and location directive setting etc etc config
Any help is very much appriciated.
Thanks.
From what I gather you want something like this
default_server Routes all non-matched connections see more
server_name abc.example.com xyz.example.com; Accepts either URL see more
location /app1 Defines a specific route for the desired server see more
server {
listen 80 default_server; # This allows any
listen [::]:80 default_server;
listen 443;
listen [::]:443;
server_name abc.example.com xyz.example.com;
location /app1 {
proxy_pass env.example.com:9060;
}
location /app2 {
proxy_pass env.example.com:9040;
}
}

nginx on localhost - wildcard domains and wildcard subdomains

I configured nginx (and dnsmasq) to listen to example.test and *.example.test wildcard subdomain. Everything seems to work fine. Here is nginx.conf:
server {
listen 80;
server_name ~(\.)?example\.test$;
...
}
Now I want to respond to all other *.test domains from their own directory on disk. I just don't know how to make it happen, following config doesn't work (just disables above configuration):
server {
listen 80;
server_name \.test;
...
}
Even following configuration has same effect:
server {
listen 80;
server_name ~(?!(\.)?example)\.test;
...
}
Both these configs work, but disables *.example.test and example.test configuration and responds to them just as other *.test domains.
Here is my question:
How can I configure nginx to respond to *.test but respond to example.test and *.example.test in a different way?
After a wasting a whole day, I finally managed to fix the issue.
For those who may find themselves in such a confusing situation, here is the solution:
# First server block for default configuration:
server {
listen 80;
server_name ~^[a-zA-Z0-9\-_]+\.test$; # matches domain names (e.g. anything.test)
...
}
server {
listen 80;
server_name ~(\.)?example\.test$ example.test; # matches all subdomains (e.g. subdomain.example.test and sub.subdomain.example.test) as well as example.test
...
}

nginx HTTPS redirect is not triggered when IP address is used

I'm using nginx as a reverse proxy for my public EC2 instance. I have:
A nice, clean public domain
An AWS-generated public domain (*.compute-1.amazonaws.com)
An AWS-generated public IP address
I would like to have all traffic go over HTTPS to the public domain. I attempted to do this by creating a "primary" server block configured to route to my application, with two secondary server blocks to catch all other traffic and redirect to https://public.domain.com. This is what my config looks like:
# "Primary" block
server {
listen 443 ssl;
server_name public.domain.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
# Other config; SSL config
}
# Catch-all redirects
server {
listen 80;
return 301 https://public.domain.com$request_uri;
}
server {
listen 443;
return 301 https://public.domain.com$request_uri;
}
In testing this, I get the following results:
http://public.domain.com >> https://public.domain.com (correct)
http://2308.compute-1.amazonaws.com >> https://public.domain.com (correct)
https://2308.compute-1.amazonaws.com >> No redirect (WRONG!)
http://55.255.255.255 >> https://public.domain.com (correct)
https://55.255.255.255 >> No redirect (WRONG!)
Why is nginx not redirecting my HTTPS traffic to my public domain? Is the server_name not used in the URL matching process?
You do not have default server set on port 443, so nginx takes the first defined host, which is server_name public.domain.com;
Use listen 443 ssl default_server, also you need a wildcard certificate for your redirect server for this config to work (self-signed, clients will show a warning anyway, if host does not match)
See https://serverfault.com/questions/578648/properly-setting-up-a-default-nginx-server-for-https

Resources