使用nginx的時候,虛擬主機和二級網域對應問題,大家是怎麼做的,例如,有兩個二級網域,對應兩個資料夾:
域名 文件夹
111.aa.com /var/www/111.aa.com
222.aa.com /var/www/222.aa.com
那麼,在設定檔中,就對應兩個server
,
#111.aa.com
#111.aa.com
server {
listen 80;
server_name 111.aa.com;
charset utf-8;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www/111.aa.com;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.php$ {
root /var/www/111.aa.com;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
#222.aa.com
#222.aa.com
server {
listen 80;
server_name 222.aa.com;
charset utf-8;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www/222.aa.com;
index index.php index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
location ~ \.php$ {
root /var/www/222.aa.com;
fastcgi_pass unix:/dev/shm/php-fpm.sock;
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
問題:
1、如果有100個二級域名,就得寫100個server
,能否寫在1個server
中呢,能否給個範例?
2、寫100個server
和1個server
,那種方式比較好呢?
天蓬老师2017-05-16 17:19:54
如果是100個不同的項目, 當然要寫100個server(會存在很多定制的情況, 最常見的就是rewrite). 如果是有一個項目對應多個域名的情況, server數量可以減少一些. 即便有方法將100個項目的配置寫在一個server裡, 那配置文件我估計也會非常複雜, 管理起來會很頭疼. 倒不如寫100個server來的方便.
配置 多 不等於 複雜
配置 多 不等於 複雜
配置 多 不等於 複雜
"多" 從來不應該是配置管理擔心的事情.
ringa_lee2017-05-16 17:19:54
如果配置都差不多,可以用nginx的變數來判斷,這樣只要寫1個配置就可以了
可參考的內容:
http://bneijt.nl/blog/post/name-based-vi...
http://www.sitepoint.com/set-automatic-v...