eldy8888
By:
eldy8888

Nginx subdomain and domain multiple server name configuration

March 1, 2016 6.8k views
Nginx

I'm trying to configure multiple server block in nginx but stuck at getting one endpoint working, below is my first site setup and it's working :

server {
    listen 80;

    server_name app.example.com;

    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

my subdomain file located at

home/app/index.html

Then I want to do for the another one, I add a folder call site means it host my static site at example.com

server {
    listen 80;

    server_name example.com;

    location /site {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }
}

my main domain file located at

home/site/index.html

I got a bad gateway 502 error.

1 Answer

The second configuration you created does not handle requests for example.com. It only routes traffic directed at example.com/site to your directory. The location line specifies a location relative to the document root in the request. You should either add a root to route other requests to static files or change location /site to location /

  • how does that help? example.com still requesting my content of home/app/index.html

    • With your current configuration nothing is pointing to the directory where your html file is located. Your example.com configuration will route traffic sent to example.com/site but there is no document root or handler for the example.com url.

      Both of these are routing traffic to a service you are running on port 3000. Neither is set up to serve any static files because there is no root specified.

      • I get you now, how to serve static content for example.com then?

        • The following configuration for example.com should do the trick (you will want to update the root directive to make sure it points to the directory with your index.html)

          server {
          listen 80;
          root /home/site/;
          index index.html;
          server_name example.com www.example.com;
          
          location / {
            try_files $uri $uri/ =404;
          }
          }
          

          This will serve static html content from the directory /home/site and use index.html as the index file to display.

          • TQ very very much!

            anyway this is the working server block with proper ;

            server {
                listen 80;
            
                server_name mcommerce.io www.mcommerce.io;
            
                root /home/site/;
                index index.html;
            
                location / {
                  try_files $uri $uri/ =404;
                }
            }
            
Have another answer? Share your knowledge.