2

I am trying to set up an angular app and a dotnet core web api on the digitalocean server. I have successfully configured the setting for these two ( at least I believe I did ). However there is this one problem - all the request whether that be maindomain.xyz or api.maindomain.xyz - every requests are handled by the api.maindomain.xyz configuration.

Is this an intended behaviour ? If its not, could you help me find a solution?

Here is the nginx configuration.


root@ubuntu-s-1vcpu-1gb-blr1-01:/etc/nginx/sites-enabled# sudo nginx -T                                                                                                
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok                                                                                                       
nginx: configuration file /etc/nginx/nginx.conf test is successful                                                                                                     
# configuration file /etc/nginx/nginx.conf:                                                                                                                            
user www-data;                                                                                                                                                         
worker_processes auto;                                                                                                                                                 
pid /run/nginx.pid;                                                                                                                                                    
include /etc/nginx/modules-enabled/*.conf;                                                                                                                             

events {                                                                                                                                                               
        worker_connections 768;                                                                                                                                        
        # multi_accept on;                                                                                                                                             
}                                                                                                                                                                      

http {                                                                                                                                                                 

# configuration file /etc/nginx/sites-enabled/api.maindomain.xyz.conf:                                                                                            
server {                                                                                                                                                               
        listen 80;                                                                                                                                                     
        server_name api.maindomain.xyz;                                                                                                                           
        return 301 https://$server_name$request_uri;                                                                                                                   
}                                                                                                                                                                      

server {                                                                                                                                                               
        listen 443;                                                                                                                                                    
        server_name api.maindomain.xyz;                                                                                                                           

        ssl_certificate           /etc/letsencrypt/live/maindomain.xyz/fullchain.pem;                                                                             
        ssl_certificate_key       /etc/letsencrypt/live/maindomain.xyz/privkey.pem;                                                                               

        ssl on;                                                                                                                                                        
        ssl_session_cache  builtin:1000  shared:SSL:10m;                                                                                                               
        ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;                                                                                                                          
        ssl_ciphers HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;                                                                                          
        ssl_prefer_server_ciphers on;                                                                                                                                  

        gzip  on;                                                                                                                                                      
        gzip_http_version 1.1;                                                                                                                                         
        gzip_vary on;                                                                                                                                                  
        gzip_comp_level 6;                                                                                                                                             
        gzip_proxied any;                                                                                                                                              
        gzip_types text/plain text/css application/json application/javascript application/x-javascript text/javascript text/xml application/xml application/rss+xml ap
lication/atom+xml application/rdf+xml;                                                                                                                                 
        gzip_buffers 16 8k;                                                                                                                                            
        gzip_disable “MSIE [1-6].(?!.*SV1)”;                                                                                                                           

        access_log  /var/log/nginx/access.log;                                                                                                                         

        location / {                                                                                                                                                   
                proxy_pass            https://localhost:5001;                                                                                                          
                proxy_http_version 1.1;                                                                                                                                
                proxy_set_header   Upgrade $http_upgrade;                                                                                                              
                proxy_set_header   Connection keep-alive;                                                                                                              
                proxy_set_header   Host $host;                                                                                                                         
                proxy_cache_bypass $http_upgrade;                                                                                                                      
                proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                         
                proxy_set_header   X-Forwarded-Proto $scheme;                                                                                                          
        }                                                                                                                                                              
}                                                                                                                                                                      

# configuration file /etc/nginx/sites-enabled/maindomain.xyz.conf:                                                                                                
server {                                                                                                                                                               
        server_name maindomain.xyz;                                                                                                                               
        listen 80;                                                                                                                                                     
        listen [::]:80;                                                                                                                                                
        return 301 https://$server_name$request_uri;                                                                                                                   
        root /var/www/html;                                                                                                                                            
        index  index.html index.htm;                                                                                                                                   
        location ~* \.(?:html|js)$ {                                                                                                                                   
                expires -1;                                                                                                                                            
        }                                                                                                                                                              
}                                                                                                                                                                      


server {                                                                                                                                                               
        listen 443 ssl;                                                                                                                                                
        listen [::]:443 ssl;                                                                                                                                           
        root /var/www/html;                                                                                                                                            
        index  index.html index.htm;                                                                                                                                   
        ssl_certificate /etc/letsencrypt/live/maindomain.xyz/fullchain.pem;                                                                                       
        ssl_certificate_key /etc/letsencrypt/live/maindomain.xyz/privkey.pem;                                                                                     
        location ~* \.(?:html|js)$ {                                                                                                                                   
                expires -1;                                                                                                                                            
        }                                                                                                                                                              
}

Removed some configuration for brevity.

4
  • Why have to put ipv6 listen address only in maindomain.xyz.conf? Commented Jul 9, 2019 at 9:14
  • @akshay202 Missed. I added that in the api.maindomain.xyz conf, however, did not make any difference in behaviour. Commented Jul 9, 2019 at 9:30
  • 2
    server_name maindomain.xyz; is missing from the last server block (listening on 443) Commented Jul 9, 2019 at 10:53
  • @Mehdi Thanks, this solved the issue. Could you please post it as an answer, so that I can accept it ? Commented Jul 9, 2019 at 11:04

1 Answer 1

1

The problem is in the last server block, where server_name is missing.

This block should contain the following, for example below the listen lines:

server_name maindomain.xyz;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.