同一服务器 Nginx 中的通配符子域和特定子域配置
Wildcard subdomain and a specific subdomain configuration in same server Nginx
我在 Nginx 默认配置中为我的 PHP app1
域设置了一个通配符子域服务器,如 *.domain.com
。如果有人输入 x.domain.com
或 y.domain.com
,我的 app1
正在服务,一切都很好。
当我为 app2
域 specific.domain.com
中的特定子域设置另一个虚拟主机时。之后,当我输入 x.domain.com 时,它显示 app2
但我只想为 specific.domain.com 显示 app2,为 *.domain.com
显示 app1
server {
# Set the port to listen on and the server name
listen 80 default_server;
server_name *.domain.com;
# Set the document root of the project
root /var/www/html/app1;
# Set the directory index files
index index.php index.html index.htm;
# Specify the default character set
charset utf-8;
# Setup the default location configuration
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# Specify the details of favicon.ico
location = /favicon.ico { access_log off; log_not_found off; }
# Specify the details of robots.txt
location = /robots.txt { access_log off; log_not_found off; }
# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile off;
client_max_body_size 128m;
# Specify what happens when PHP files are requested
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name specific.domain.com;
root /var/www/html/app2;
index index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ /index.html$is_args$args;
}
}
刚刚解决了这个问题,添加了更多的 1 个服务器块并先 specific.domain.com
然后 *.domain.com
。我将默认服务器块留空
这是我的代码示例
server {
listen 80 default_server;
server_name *.domain.com;
root /var/www/html;
}
server {
listen 80;
server_name specific.domain.com;
root /var/www/html/app2;
}
server {
listen 80;
server_name *.domain.com;
root /var/www/html/app2;
}
我在 Nginx 默认配置中为我的 PHP app1
域设置了一个通配符子域服务器,如 *.domain.com
。如果有人输入 x.domain.com
或 y.domain.com
,我的 app1
正在服务,一切都很好。
当我为 app2
域 specific.domain.com
中的特定子域设置另一个虚拟主机时。之后,当我输入 x.domain.com 时,它显示 app2
但我只想为 specific.domain.com 显示 app2,为 *.domain.com
server {
# Set the port to listen on and the server name
listen 80 default_server;
server_name *.domain.com;
# Set the document root of the project
root /var/www/html/app1;
# Set the directory index files
index index.php index.html index.htm;
# Specify the default character set
charset utf-8;
# Setup the default location configuration
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# Specify the details of favicon.ico
location = /favicon.ico { access_log off; log_not_found off; }
# Specify the details of robots.txt
location = /robots.txt { access_log off; log_not_found off; }
# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
sendfile off;
client_max_body_size 128m;
# Specify what happens when PHP files are requested
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors off;
fastcgi_buffer_size 16k;
fastcgi_buffers 4 16k;
}
# deny access to .htaccess files
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name specific.domain.com;
root /var/www/html/app2;
index index.html index.htm;
charset utf-8;
location / {
try_files $uri $uri/ /index.html$is_args$args;
}
}
刚刚解决了这个问题,添加了更多的 1 个服务器块并先 specific.domain.com
然后 *.domain.com
。我将默认服务器块留空
这是我的代码示例
server {
listen 80 default_server;
server_name *.domain.com;
root /var/www/html;
}
server {
listen 80;
server_name specific.domain.com;
root /var/www/html/app2;
}
server {
listen 80;
server_name *.domain.com;
root /var/www/html/app2;
}