我想用两个不同的子域运行nginx,让它们指向不同的根。因此,我将两个服务器块拆分为文件,相应地命名它们并将其设置为如下所示。当我禁用其中一个子域时,两个子域都指向同一个根,因此它们为自己工作,但是sub1和sub2之间的划分不起作用。它只使用它首先为两个子域找到的文件。
sub1.example.de
server {
server_name sub1.example.de;
root /var/www/wordpress/;
index index.html index.htm index.php;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location ~\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_cache_key $scheme$host$request_uri$request_method;
fastcgi_cache_valid 200 301 302 30s;
fastcgi_cache_use_stale updating error timeout invalid_header http_500;
fastcgi_pass_header Set-Cookie;
fastcgi_pass_header Cookie;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param REQUEST_URI $request_uri;
}
location / {
try_files $uri $uri/ /index.php;
}
location /wordpress/ {
try_files $uri $uri/ /wordpress/index.php;
}
}
sub2.example.de
server {
server_name sub2.example.de;
root /var/www/phpmyadmin/;
index index.html index.htm index.php;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location ~\.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
}
location / {
try_files $uri $uri/ /index.php;
}
}
我的nginx.conf看起来像这样:
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024 ;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
map $scheme $server_https {
default off;
https on;
}
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}