Configuring nginx to use www.domain.com instead of domain.com

I had a client who had their local server setup under domain.com instead of properly under local.domain.com or something similar. It caused a bit of a headache (you could view their new site from outside their local office, but in their local office the old website was still showing.)

Their local server guy (thanks Will) helped me solve the issue. Their website needed to use only www.

Here’s his notes:

Company XYZ Mortuary has two separate DNS systems

1. The public-facing DNS server that you control

2. The internal DNS server for Company XYZ’s private network that we control

Since both of these servers are authoritative for the CompanyXYZ.com domain, both need to be updated when IP addresses of public servers change.

The other issue we had was the web site removing the www from the URL, which caused computers on Company XYZ’ private network to access an internal server. Windows domain controllers reserve the IP address for the domain name for themselves, and modifying this behavior breaks functionality such as DFS and Exchange. For this reason, Microsoft and other OS vendors recommend using a TLD that will never be used on the Internet, such as .local.

And here is the adjusted nginx config file to use www & point appropriately.

The key line(s) are:
server_name company-xyz.com;
rewrite ^/(.*) http://www.company-xyz.com/$1 permanent;

upstream domain4 {
        server 127.0.0.1:3400;
        }

server {
            listen   80;
            server_name company-xyz.com;
            rewrite ^/(.*) http://www.company-xyz.com/$1 permanent;
           }

server {
            listen   80;
            server_name www.company-xyz.com;

            access_log /home/username/public_html/railsapp/shared/log/access.log;
            error_log /home/username/public_html/railsapp/shared/log/error.log;

            root   /home/username/public_html/railsapp/current/public/;
            index  index.html;

            location / {
                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect false;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

                          if (!-f $request_filename) {
                                           proxy_pass http://domain4;
                                           break;
                          }
            }

}

server {

            listen   443;
            ssl on;
            ssl_certificate     /etc/ssl/certs/company-xyz.com.crt;
            ssl_certificate_key   /etc/ssl/private/companyxyz.key;       

            server_name  company-xyz.com;
            rewrite ^/(.*) http://www.company-xyz.com/$1 permanent;

           }

server {

            listen   443;
            ssl on;
            ssl_certificate     /etc/ssl/certs/company-xyz.com.crt;
            ssl_certificate_key   /etc/ssl/private/companyxyz.key;

            server_name www.company-xyz.com;

            access_log /home/username/public_html/railsapp/shared/log/access.log;
            error_log /home/username/public_html/railsapp/shared/log/error.log;

            root   /home/username/public_html/railsapp/current/public/;
            index  index.html;

            location / {
                          proxy_set_header X_FORWARDED_PROTO https;

                          proxy_set_header  X-Real-IP  $remote_addr;
                          proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
                          proxy_set_header Host $http_host;
                          proxy_redirect false;

                          if (-f $request_filename/index.html) {
                                           rewrite (.*) $1/index.html break;
                          }

                          if (-f $request_filename.html) {
                                           rewrite (.*) $1.html break;
                          }

                          if (!-f $request_filename) {
                                           proxy_pass http://domain4;
                                           break;
                          }
            }

}

Comments