Sunday, 30 April 2017

Configure root domain and subdomain in Nginx with SSL

I want to point example.com to localhost:3000 and api.example.com to localhost:3010. Following this tutorial I managed to get it to work with only domain.com but I don't know how to configure the other one.

Here's my default sites Nginx config (the one in /etc/nginx/sites-enabled/default):

server {                                                                           
        listen 80;                                                                 
        listen [::]:80 example.com ipv6only=on;                                
        return 301 https://example.com$request_uri;                            
}                                                                                  

server {                                                                           
        # Enable HTTP/2                                                            
        listen 443 ssl http2;                                                      
        listen [::]:443 ssl http2;                                                 
        server_name example.com;                                               

        # Use SSL certificates from Letsencrypt                                    
        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;       
        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;     

        # Include SSL config from cipherli.st                                      
        include snippets/ssl-params.conf;                                          

        location / {                                                               
                proxy_set_header X-Real-IP $remote_addr;                           
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;       
                proxy_set_header X-NginX-Proxy true;                               
                proxy_pass http://localhost:3000/;                                 
                proxy_ssl_session_reuse off;                                       
                proxy_set_header Host $http_host;                                  
                proxy_cache_bypass $http_upgrade;                                  
                proxy_redirect off;                                                
        }                                                                          
}                                                                                  

server {                                                                           
        listen 80;                                                                 
        listen [::]:80 api.example.com ipv6only=on;                                  
        return 301 https://api.example.com$request_uri;                              
}                                                                                  

server {                                                                           
        listen 443 ssl http2;                                                      
        listen [::]:443 ssl http2;                                                 
        server_name api.example.com;                                                 

        ssl_certificate /etc/letsencrypt/live/api.example.com/fullchain.pem;         
        ssl_certificate_key /etc/letsencrypt/live/api.example.com/privkey.pem;       

        include snippets/ssl-params.conf;                                          

        location / {                                                               
                proxy_set_header X-Real-IP $remote_addr;                           
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;       
                proxy_set_header X-NginX-Proxy true;                               
                proxy_pass http://localhost:3010/;                                 
                proxy_ssl_session_reuse off;                                       
                proxy_set_header Host $http_host;                                  
                proxy_cache_bypass $http_upgrade;                                  
                proxy_redirect off;                                                
        }                                                                          
}                                                                                  

Could someone please suggest how I fix this, works perfectly fine with only one domain.



via Vlady Veselinov

No comments:

Post a Comment