0

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 the server blocks is the server_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
  • 103
  • 1
  • 6
  • Your question isn't clear, and doesn't have resources required to answer. Please 1) Edit your question to clearly say what you're trying to achieve, and 2) Include all Nginx server configurations - you can cut out the locations, but leave in at least the server_name and listen directives. – Tim Apr 25 '17 at 21:25
  • @Tim per your suggestion, I edited my question to include the entirety of both server block. All I'm trying to accomplish is have two different registered urls point to the same website. – Matt Cremeens Apr 25 '17 at 22:33
  • If that's all you want then just add all URLs to the server_name directive of one server : "server_name example.com example2.com www.example.com". I don't see how you could get a conflict if the only servers are the ones you've included above. That error should only be shown if you have multiple server blocks with the same server_name and listen port. – Tim Apr 25 '17 at 22:49
  • I'm a little surprised by the warning myself. I'll try as you suggest and let you know what happens. I appreciate you getting back to me. – Matt Cremeens Apr 25 '17 at 22:52
  • @Tim Well, I'm not sure this is progress or not, but I no longer get those warnings. However, I am still getting the 'Welcome to nginx!' message when I visit the new url. I will say that `service nginx restart` is failing even though `sudo nginx -t` is saying everything looks a-okay. – Matt Cremeens Apr 25 '17 at 23:17
  • It's difficult to say, given how ambiguous your question is. More information would be required to help. Full configurations, error messages during test / start, logs, etc. – Tim Apr 25 '17 at 23:22
  • I'm not getting any errors. The site works fine for the first url. All I want is 2 different urls to access one site (the site that already works just fine). The only file I've changes is my `conf` file in both `sites-available` and `sites-enabled`. – Matt Cremeens Apr 25 '17 at 23:24
  • Do you think it has anything to do with the ssl certificate I'm using? – Matt Cremeens Apr 25 '17 at 23:26
  • Could be, certificates are URL specific. You've set things up really strangely. It's rare to serve http and https with the same server. Usually people have a separate http server that simply forwards all traffic to http. Because you haven't done that diagnosis is more difficult. At this point I don't even understand what problem you're having. – Tim Apr 25 '17 at 23:41

3 Answers3

1

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.

Tero Kilkanen
  • 36,796
  • 3
  • 41
  • 63
  • I did as you suggested and received this: `start-stop-daemon: warning: failed to kill 1006: Operation not permitted` – Matt Cremeens Apr 27 '17 at 11:20
  • And you are running it with root permissions? – Tero Kilkanen Apr 28 '17 at 06:25
  • OK, 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 Apr 28 '17 at 10:02
  • Now 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 Apr 28 '17 at 10:04
  • That 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. – Matt Cremeens Apr 28 '17 at 10:20
0

I think it may make more sense to:

  • have one minimal server block for your HTTP site, which (most likely) will only redirect to HTTPS
  • and have a server block for your HTTPS site

Additionally, you might want to consider if you want to have a single HTTPS site, or two (i.e. one for each of your domains) - in other words, you could have both HTTP server blocks look something like:

server {
    listen   80;
    server_name beta.barefootretirement.com beta.gowealthretirement;
    return 301 https://beta.barefootretirement.com$request_uri;
}

Or you could create a similar block for dealing with HTTPS requests for both domains (depending on what your needs are).

I am not totally clear what exactly is causing your issue (and can't dig into that right now), but I think that reducing redundancy in your config certainly can't hurt.

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

One small thing about this comment: generally, you edit the sites-available file, and you make the site enabled by symlinking that file into /etc/nginx/sites-enabled - I get the sense you have two files currently.

This is probably not relevant to your issue, but here too, it can't hurt to remove unnecessary things.

iwaseatenbyagrue
  • 3,688
  • 15
  • 24
0

Why not just do this?

server {
    server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;
    listen 80;
    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;
        }


    }
}

All you need is one server directive with both the server names on one line... Also, if statements are generally not recommended for performance reasons, better to do:

server {
    server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;

    listen 80;
    listen [::]:80;

    return 301 https://$server_name$request_uri;
}

server {
    server_name beta.portal.barefootretirement.com beta.gowealthpoint.com;

    listen 443 ssl;
    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;

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


    }
}
NotoriousPyro
  • 260
  • 1
  • 5
  • What 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 Apr 26 '17 at 12:02
  • Do you think it is a problem that I already have a default listening on port 80 and [::]:80? – Matt Cremeens Apr 26 '17 at 14:39
  • I tried your first block of server code and I am just getting the 'Welcome to nginx! ... ` page when I visit from the gowealthpoint url. :( – Matt Cremeens Apr 26 '17 at 14:45
  • Post your entire nginx config because something is breaking whatever you're trying to do. – NotoriousPyro Apr 26 '17 at 23:07
  • per 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. – Matt Cremeens Apr 27 '17 at 11:31
  • Have 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? – NotoriousPyro Apr 28 '17 at 11:49
  • I 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 then `sudo service nginx start`. – Matt Cremeens Apr 28 '17 at 11:52