Nginx 主域请求被转发到子域
Nginx main domain requests getting forwarded to subdomain
我正在尝试在 digitalocean 服务器上设置一个 angular 应用程序和一个 dotnet 核心网络 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;
}
}
为简洁起见删除了一些配置。
问题出在最后的 server
块中,其中缺少 server_name
。
此块应包含以下内容,例如 listen
行下方:
server_name maindomain.xyz;
我正在尝试在 digitalocean 服务器上设置一个 angular 应用程序和一个 dotnet 核心网络 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;
}
}
为简洁起见删除了一些配置。
问题出在最后的 server
块中,其中缺少 server_name
。
此块应包含以下内容,例如 listen
行下方:
server_name maindomain.xyz;