我想使用多个 php 应用程序和配置文件来处理多个域,但我愿意在 nginx 中处理它,而不是在 php 应用程序中。
第一个问题是关于 nginx 中 1000 个域名为别名与 1000 个域名分开时的性能。哪个更好?
另一个问题是如何通过域名获得一些配置文件并通过参数触发不同的 php 脚本
例如:
domain1.com
app: Wordpress
domain2.com
app: Drupal
然后在 nginx.php 指令中我会有:
try_files $uri $uri/ /{app}/index.php?$args;
答案1
这应该正是您正在寻找的:
http {
...
# custom log format to include the domain
log_format main '$remote_addr - $host [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent"';
map $host $app {
default "wordpress";
"~domain.com" "drupal";
"~blog.other.com" "wordpress";
"~shop.store.com" "magento";
}
}
server {
server_name ~^(www\.)?(?<domain>.+)$; # expects site.com folder and accepts both www and non-www
root /home/www/sites/$domain/$app;
access_log /var/log/nginx/access.log main;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_param SERVER_NAME $host; # normally you would get the server_name regex
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PHP_ADMIN_VALUE "open_basedir=$document_root:/tmp/:/usr/share/php"
fastcgi_pass unix:/var/run/php-fpm/www.sock;
}
}