简体   繁体   English

Nginx 主域请求被转发到子域

[英]Nginx main domain requests getting forwarded to subdomain

I am trying to set up an angular app and a dotnet core web api on the digitalocean server.我正在尝试在 digitalocean 服务器上设置一个 angular 应用程序和一个 dotnet 核心 web api。 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.然而,有一个问题——所有请求,无论是 maindomain.xyz 还是 api.maindomain.xyz——每个请求都由 api.maindomain.xyz 配置处理。

Is this an intended behaviour ?这是预期的行为吗? If its not, could you help me find a solution?如果不是,您能帮我找到解决方案吗?

Here is the nginx configuration.这是nginx配置。


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.为简洁起见,删除了一些配置。

The problem is in the last server block, where server_name is missing.问题出在最后一个server块中,其中缺少server_name

This block should contain the following, for example below the listen lines:此块应包含以下内容,例如在listen行下方:

server_name maindomain.xyz;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM