Pointing 2 urls to the same site on nginx server
If nginx -t
runs correctly, but nginx restart fails, then there might be an nginx process running without correct PID file.
This means that stopping the nginx process fails, and when system tries to start it, it fails because it is already running. In this case nginx still uses the old configuration.
Try service nginx stop
, check if nginx is running with ps
command. If it is still running, use kill
to stop it, and try service nginx start
after that.
Related videos on Youtube
Matt Cremeens
I enjoy experiences where computer science meets mathematics and invention in useful and astonishing ways.
Updated on September 18, 2022Comments
-
Matt Cremeens over 1 year
We have this website that works well already, but I was put to the task of pointing another url to that very same site. The idea is that two urls will show in the address bar, but just one site will be on display.
I added an A-record to my DNS management site to point the new domain name to the IP address of the server where my old site is located and receive the traditional page that says
"Welcome to nginx! If you see this page, ..."
So in researching how to configure my server, I came across this site which looked promising and made the task seem simple enough.
So what I did was duplicate my
server { ... }
in my/etc/nginx/sites-available/beta.conf
and in my/etc/nginx/sites-enabled/beta.conf
like so (notice that the only difference between theserver
blocks is theserver_name
).nginx.conf: user www-data; worker_processes 4; pid /run/nginx.pid; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; ## # nginx-naxsi config ## # Uncomment it if you installed nginx-naxsi ## #include /etc/nginx/naxsi_core.rules; ## # nginx-passenger config ## # Uncomment it if you installed nginx-passenger ## #passenger_root /usr; #passenger_ruby /usr/bin/ruby; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } } default: server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; # Make site accessible from http://localhost/ server_name localhost; location / { # First attempt to serve request as file, then # as directory, then fall back to displaying a 404. try_files $uri $uri/ =404; # Uncomment to enable naxsi on this location # include /etc/nginx/naxsi.rules } } upstream beta_app_server { server unix:/home/beta/run/gunicorn.sock fail_timeout=0; } server { listen 80; server_name beta.portal.barefootretirement.com; listen 443 ssl; ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem; if ($scheme = http) { return 301 https://$server_name$request_uri; } client_max_body_size 4G; access_log /home/beta/logs/nginx-access.log; error_log /home/beta/logs/nginx-error.log; location /static/ { alias /home/beta/static/; } location /media/ { alias /home/beta/media/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://beta_app_server; break; } } } server { listen 80; server_name beta.gowealthpoint.com; listen 443 ssl; ssl_certificate /etc/letsencrypt/live/beta.portal.barefootretirement.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/beta.portal.barefootretirement.com/privkey.pem; if ($scheme = http) { return 301 https://$server_name$request_uri; } client_max_body_size 4G; access_log /home/beta/logs/nginx-access.log; error_log /home/beta/logs/nginx-error.log; location /static/ { alias /home/beta/static/; } location /media/ { alias /home/beta/media/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto https; proxy_set_header Host $http_host; proxy_redirect off; if (!-f $request_filename) { proxy_pass http://beta_app_server; break; } } }
I go to test to see if this configuration is going to work with
sudo nginx -t
but I get these warnings
nginx: [warn] conflicting server name "beta.gowealthpoint.com" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "beta.gowealthpoint.com" on 0.0.0.0:443, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful
and when I try to restart the server
service nginx restart
it fails.
No temp files have been left behind in either location. I checked using
ls -lah
It's clear I'm doing something wrong, but I don't know what. Any help would be greatly appreciated.
-
Matt Cremeens about 7 yearsWhat I ended up doing late last night was forwarding the other site with masking to the main site and that also seemed to work, though I wouldn't call it ideal. I am going to try your solution, though, and let you know how it goes. It means a lot to me that you would try to help.
-
Matt Cremeens about 7 yearsThanks for the bit on the symnlink. I'll try to work that into a solution. Will keep you posted and am grateful for your feedback and attempt to help me.
-
Matt Cremeens about 7 yearsDo you think it is a problem that I already have a default listening on port 80 and [::]:80?
-
NotoriousPyro about 7 yearsPost your entire nginx config because something is breaking whatever you're trying to do.
-
Matt Cremeens about 7 yearsI did as you suggested and received this:
start-stop-daemon: warning: failed to kill 1006: Operation not permitted
-
Matt Cremeens about 7 yearsper your suggestion, I added more conf details, including my default, extra details of beta.conf at the top and the contents of my nginx.conf. Maybe that'll help.
-
Tero Kilkanen about 7 yearsAnd you are running it with root permissions?
-
Matt Cremeens about 7 yearsOK, I did that, but the result I've been having is the same. :( I would really like to get this working for my boss. I wouldn't have thought this to be quite so troublesome, but I'll admit I don't know much about server configuration.
-
Matt Cremeens about 7 yearsNow this is a little interesting. Following your directive made my gowealthpoint url produce a server 500 error whereas before I was getting the 'Welcome to nginx ...' page.
-
Matt Cremeens about 7 yearsThat was it. Restarting nginx was not working. I had to bring it to a full stop and then run start, precisely as you said. You will never know how grateful I am.
-
NotoriousPyro about 7 yearsHave you tried setting beta_app_server as an upstream? Like so: upstream beta_app { server beta_app_server:80}? I'm thinking this may be something to do with whatever you're proxying to is changing the way Nginx is responding?
-
Matt Cremeens about 7 yearsI really appreciate your help. Please refer to the accepted answer. Using
service nginx restart
was not working properly and so I needed to restart in two steps:sudo service nginx stop
and thensudo service nginx start
.