53

I want to run www.example.com and api.example.com on same port 80.

This is what I have. All my googles ping lead to the below code. But, this is not working.

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        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
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

server {
        listen 80
        server_name api.example.com

        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

I do not know the reason. Any suggestions on this?

1
  • 3
    The root dir is missing in the api.example.com virtual host.
    – IamK
    Commented Oct 10, 2015 at 15:00

2 Answers 2

38

Create separately two files (you don't have to, but it will be much clearer) in /etc/nginx/sites-available/www.example.com and /etc/nginx/sites-available/api.example.com

The api.example.com file's content:

server {
        listen 80;
        server_name api.example.com;
        root /var/www/api.example.com/html/example/app; #also add a root dir here
        location / {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

The www.example.com's content:

server {
        listen 80 default_server;
#       listen [::]:80 default_server ipv6only=on;

        root /var/www/example.com/html/example/app;
        index index.html index.htm;

        # Make site accessible from http://localhost/
        server_name www.example.com www.example.org;

        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
        }

        location /bower_components {
                alias /var/www/example.com/html/example/bower_components;
        }

        location /scripts {
                alias /var/www/example.com/html/example/scripts;
        }

        location /content {
                alias /var/www/example.com/html/example/content;
        }

        location /api {
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_pass http://127.0.0.1:3836;
        }
}

And finally enable the domains: sudo ln -s /etc/nginx/sites-available/www.example.com /etc/nginx/sites-enabled/www.example.com and sudo ln -s /etc/nginx/sites-available/api.example.com /etc/nginx/sites-enabled/api.example.com

6
  • I am not able to understand what do you mean by "Also add the www.example.com virtual host to you config file:". Could you explain more?
    – user405398
    Commented Oct 10, 2015 at 14:49
  • 1
    If this doesn't work what is the output of the tail -f /var/log/nginx/error.log command if you try to reach the webpage?
    – IamK
    Commented Oct 10, 2015 at 14:56
  • Thank you so much. Sorted it out.
    – user405398
    Commented Oct 10, 2015 at 15:14
  • 55
    -1 This just a convenience. You don't have to separate servers blocks in multiple files, and it doesn't solve any configuration issues you'd have except any problems due to nginx loading the confs in the wrong order (which you are at least as likely to cause).
    – Albin
    Commented Oct 27, 2016 at 8:22
  • I found when I tried this, that nginx just chose the default_server and ignored the other one. not sure why Commented Oct 9, 2021 at 0:14
6

The initial questioned config was missing semicolons at the end of the lines for listen and server_name. This could cause it to not be read.