Nginx主域请求转发到子域

时间:2019-07-09 08:11:31

标签: nginx nginx-reverse-proxy nginx-config

我正在尝试在digitalocean服务器上设置一个角度应用程序和一个dotnet核心Web API。我已经成功配置了这两个设置(至少我相信是的)。但是,存在一个问题-所有请求(无论是maindomain.xyz还是api.maindomain.xyz)-每个请求都由api.maindomain.xyz配置处理。

这是预期的行为吗?如果没有,您能帮我找到解决办法吗?

这是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;                                                                                                                                            
        }                                                                                                                                                              
}

为简洁起见,删除了一些配置。

1 个答案:

答案 0 :(得分:1)

问题出在server的最后一个server_name块中。

此块应包含以下内容,例如在listen行下方:

server_name maindomain.xyz;