I have an NGINX webserver, which is serving multiple websites. For each website, I have a seperate nginx configuration file in the /etc/nginx/sites-available folder.
Now, I would like to have two domainnames (aaa.com and bbb.com) point to the same website. On the registrar side both domain names point to the IP address of my webserver.
On nginx, I have the following configuration:
server {
listen 443 ssl http2;
root /var/www/html/aaa.com/public_html;
index index.html index.php index.htm;
server_name aaa.com www.aaa.com bbb.com www.bbb.com;
<rest of the configuration
}
I can reach now www.aaa.com but accessing the website via bbb.com does not work.
Should I create a seperate config file (in sites-available) for bbb.com and point to the location of the website in /var/www/html and enable it in sites-enabled?
Related
I’m trying to host 2 different websites - one static non-wordpress site, and one wordpress subdomain site - on my own pi server (test sites). Whenever accessing the subdomain site test.mysite.co.uk, it instead loads test.mysite.co.uk:4323 at the unwanted port 4323. The main mysite.co.uk site loads correctly however.
Initially I’ve been running these test sites locally (on different ports - the main site on port 4321 and subdomain on 4323) until I decided to deploy them using real domain names. However, presumably you cannot configure DNS to point to a specific IP and port (presumably a DNS record just points to an IP only), so I changed both the 2 domains’ conf files to listen to port 80 (as apparently you can define the server names to tell nginx which site to load - called virtual hosts?). Note that I have DNS A records for mysite.co.uk and test.mysite.co.uk that both point to the same public IP address of my router.
Nowhere is there a reference to port 4323 anymore, so I am confused as to why the subdomain still insists on forwarding to that port. I’ve been using incognito mode on chrome so there should be no caching issues. My router forwards external port 80 to internal port 80, and I’ve restarted the nginx server multiple times. The default port of my pi itself is no longer 80.
Here’s the /etc/nginx/sites-available/mysite.co.uk.conf file:
server {
listen 80;
listen [::]:80;
root /var/www/mysite.co.uk;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name mysite.co.uk www.mysite.co.uk;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
}
And here’s the /etc/nginx/sites-available/test.mysite.co.uk.conf file:
upstream wp-php-handler {
server unix:/var/run/php/php7.4-fpm.sock;
}
server {
listen 80;
server_name test.mysite.co.uk;
root /var/www/wp.mysite.co.uk;
index index.php;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass wp-php-handler;
}
}
Both .conf files are also symlinked to their respective sites-enabled folders.
Not sure if this means anything but loading local_ip:80 (or without :80 as presumably it assumes :80) in a web browser returns the Apache2 Ubuntu Default Page.
As per this post, I’ve tried adding port_in_redirect off, autoindex on and proxy_redirect http://test.mysite.co.uk:4323/ http://test.mysite.co.uk/ but to no avail.
Does anyone have any ideas of what I’m doing wrong?
UPDATE:
I've managed to create another test non-wordpress site that's exactly the same as the first non-wordpress site but called copy.mysite.co.uk, which seems to work. I'm assuming the problem with the wordpress test site may be to do with its config (although I can't see anything wrong with the code I've listed here)?
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 do I setup nginx conf so that "blah.com/website1" and "blah.com/website2" etc can point to separate websites that are running on the machine (vhost?)?
E.g. localhost:3000 is website1 and localhost:3001 is website2.
So when a user points their browser at blah.com/website1 they access the website localhost:3000 and localhost:3001 for website2?
I've tried a reverse proxy but that can't map paths like "blah.com/website1" and only seems to handle the server name of "blah.com".
Update:
So I've tried a config like this to use reverse proxy, but all assests on the website are not loading as they are trying to load from the domain's root. Probably because it's not a vhost? The page loads but assets are trying to load and 404ing, e.g: http://blah.com/static/css/styles.css
server {
listen 80;
server_name blah.com;
location /website1/ {
proxy_pass http://localhost:3001/;
}
}
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.
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.