Point multiple domain to one virtual host in Nginx - nginx

Basically, I want to redirect every domain name to one virtual host, the documentation seems very clear.
server {
listen 80;
server_name domain1.com www.domain.com domain2.com www.domain.com;
...
}
However, only the first domain1.com works. The rest, www.domain1.com, domain2.com and www.domain2.com don't work at all. Am I doing something wrong? Is there any other settings to input in order to use multiple domains? Thanks!

Well the config is right, so you either didn't reload the settings
sudo service nginx reload
or the domains don't point to the server, check
user#host:~$ host domain.com
user#host:~$ host www.domain.com
make sure that the IP's match

Related

Nginx subdomain: redirecting to wrong directory

I have a domain (let it be example.com). Also, I have a configured nginx web server, where example.com is pointed to the root directory /var/www/example.
Then, I wanted to point service.example.com to /var/www/example, and then point the original example.com to /var/www/service
Fisrt lines of nginx config for service.example.com look like this:
server {
server_name service.example.com;
root /var/www/example;
......
It works, and when I try to access service.example.com, I'm being redirected to the index file in /var/www/example.
Then, I made a config for example.com:
server {
server_name example.com www.example.com;
root /var/www/service;
......
,but when I'm trying to access example.com, I see the index file of /var/www/example— as if I tried to access service.example.com.
So, how can I solve this problem, and see index file of /var/www/service when I'm trying to access example.com?
Problem was in symlinks.
Some of them was not the symlinks in sites-enabled, but archive. So, nginx didn't handled them.
Problem was solved!
server {
root /var/www/service;
server_name service.example.com;
}
reference: https://hackprogramming.com/how-to-setup-subdomain-or-host-multiple-domains-using-nginx-in-linux-server/

How can i get domain-name instead of ip-address using nginx in ubuntu?

I complete the configuration in nginx as well as i connect it via this link
http://1.2.3.4/MyProject/
where my ip address assign in nginx 1.2.3.4
and my nginx configuration
server_name 1.2.3.4;
listen 80;
but when i give domain name like
server name account.com;
listen 80;
but when i open that page from browser like:- http://account.come it will redirect at http://:1.2.3.4/myproject
so how can i hide my ip-address and see my domain name?
Thankyou so much.
remove any server_name 1.2.3.4; in all config files and also see default config file under site-availeables
to check why its redirect to ip please check yor DNS server for example bind or bind9 service or named: /etc/named and all zones under /var/lib/named
maybe new site not configured proper by you then ping sitename once in your client system like windows and once by lynx on server via putty...
2-check nginx is working with proxycache or module cache ??
3-check your project code setting for example wordpress or some projects redirect some pages to cached url on database...
to check is from this project make test.php and code about echo rand(1,999); that can test redirection and cache at once.
ifrandom code show same number it is cache problem.
if no cache but redirecting to server ip address it is configuration issue...
if not solved remove all http server configs and define again .
add with www alias for site name
server {
listen 80 ;
server_name a.com www.a.com;
I had got the solution for that, I changed my godady account configuration.
and then put like:-
server_name abc.com;

All requests to same directory, regardless of domain - NGINX

WordPress Multisite
Nginx
WPMU Domain Mapper
(I think my question has a non-WP-specific solution.)
Domains that point at my server's IP address don't get sent to my WordPress site unless they are defined in the config file or the domain is defined in /sites-enabled
So, if I just set the A-record of some random example.com domain to my IP, I get a "Welcome to nginx on Debian!" message.
But I need it to go directly to the WordPress directory.
And I need it to happen anytime anyone sets up a domain to point at the IP address. That is --- I need to set up the config so all requests go to the WordPress directory.
EDIT:
I have tried to set the default directory to /var/www/wordpress --- but that didn't seem to work.
You can use regex in nginx/conf.d/ config, to catch domains:
server {
listen 80;
set $location_root "/var/www/";
server_name ~^(?<somedomain>[\w-\.]+\.com)$;
root $location_root/$somedomain;
include conf.d/wpconf;
}
If you have domains not only in *.com area, just fix regex. For example, for domain site.com, this regex will go to /var/www/site.com directory.
Note: including conf.d/wpconf is not necessary, because i store in that file specific options for WPsites.
The solution was simple:
server {
listen 80 default_server;
server_name myprimarydomain.com *.myprimarydomain.com;
server_name_in_redirect off;
root /var/www/wordpress;
.
.
.
If you followed the Nginx/Multisite guide from WPMU --- you also have to remove the default file in the sites-available directory. It is being included by a line in the confd file, and it contains a conflicting default_server line.

nginx with 2 domains pointing to same root

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;
}

How to test nginx subdomains on localhost

I want to test nginx subdomains before uploading config to the server. Can i test it on localhost? I try
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://localhost:8080;
}
}
server {
listen 80;
server_name sub.localhost;
location / {
proxy_pass http://localhost:8080/sub;
}
}
And it does not work. Shoulld i change my hosts file in order to make it work? Also, after uploading site to the server should i change DNS records and add sub.mydomain.com?
Yes, add '127.0.0.1 sub.localhost' to your hosts file. That sub has to be resolved somehow. That should work.
Then once you're ready to go to the net, yes, add an a or cname record for the subdomain sub.
When I use proxy_pass I also include the proxy.conf from nginx.
http://wiki.nginx.org/HttpProxyModule
In Linux based OS just to edit as sudo /etc/hosts file and change 127.0.0.1 localhost to 127.0.0.1 *.localhost.
So at /etc/nginx/sites-enabled/<environment>/<your_project_name> edit server_name key as <subdomain>.localhost.
Reload nginx and networking service.
$ sudo service nginx reload
$ sudo service networking reload
And then try http://<subdomain>.localhost at url bar.
It works for me.
UPDATE
In my opinion, a better solution is creating a virtual server that only responds if subdomain doesn’t exist, at /etc/nginx/sites-enabled/development/default, as default server (remember that you can define only one server as default).
server {
listen 80 default_server;
root /var/www/html/errors/404;
server_name *.localhost *.<host-name>;
location / {
index subdomain.html;
}
}
Make sure that in nginx.conf (generally at /etc/nginx/nginx.conf) contain include /etc/nginx/sites-enabled/**/*; to this virtual server work. If not, put it and then run $ sudo service nginx reload.
In this case isn't necessary put *.localhost in /etc/hosts, but only localhost.
For your public webserver with its own domain name, you just need to add a Canonical name using a CNAME record in your DNS configuration:
CNAME * example.com.
Once this is done, set your nginx setting
server_name *.example.com example.com;
In your local setup you can keep the same configuration for nginx but unless you have a local DNS setup, you will have to edit your /etc/hosts file and add each subdomain manually. wildcards don't work in the /etc/hosts file.
127.0.0.1 abc.example.com def.example.com ghi.example.com
It is generally recommended to use .local as the namespace for your local domains.
With an Nginx configuration like shown by the OP, all that is needed is to configure the local DNS resolution. I run Linux containers on a VM with a local DHCP IP but test them on Windows 10 browsers.
The DNS configuration can be done by editing "C:\Windows\System32\drivers\etc\hosts" as Administrator.
192.168.100.50 sub.example.local
192.168.100.50 example.local
Of course, use 127.0.0.1 or other appropriate IP as needed.

Resources