Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx multiple server blocks listening to same port

Tags:

nginx

People also ask

Can NGINX have multiple server?

If you are using a dedicated server / VPS and want to host multiple applications on a separate domain and a single server then you will need to host multiple websites on a single server. You can achieve this with Apache / Nginx virtual hosting. Virtual hosting allows you to use a single VPS to host all your domains.

Can NGINX listen on multiple ports?

To make Nginx Listen on multiple ports for a single virtual host file, you can add multiple listen directives. If you want to make Nginx listen for different virtual hosts on different ports, you can use different ports in listen directive in different virtual host files. It's that easy!

What is server_name _ in NGINX?

server { listen 80; server_name example.org www.example.org ""; ... } If no server_name is defined in a server block then nginx uses the empty name as the server name. nginx versions up to 0.8. 48 used the machine's hostname as the server name in this case. If a server name is defined as “ $hostname ” (0.9.


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 conten

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