nginx multiple server block in same port
Posted On 2016年2月27日
花了半个晚上的时间终于把apache http server 迁移至 nginx + php-fpm.
nginx 一个端口多个server配置的实现。
测试nginx版本: 1.6.3 说在前面的话,因为没有编译最新的nginx,所以如下所陈述的方法很可能在新版本上不正常。 另外,所提到的问题,也可能已经修复。 (因为编译需要依赖一些其他的devl 包,所以为了节省时间快速迁移,直接yum 安装了所有需要的包)
截至到2016-2-27日,nginx最新版本 1.9.12
#server 1 文件命名为 /etc/nginx/conf.d/site1.conf
server {
listen 80;
server_name example.net www.example.net;
root /mysite/site1;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
charset utf-8;
index index.php index.html index.htm index.do;
location / {
}
}
#server 2 的配置文件位于 /etc/nginx/conf.d/site2.conf
server {
listen 80;
server_name mysite.net www.mysite.net;
root /mysite/site2;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
charset utf-8;
index index.php index.html index.htm index.do;
location / {
}
}
在1.6.3 版本的试验当中,发现第二个site的 server block里的root 设置不能生效,需要在 location /{ 中在额外生命一次的 root 路径}
即
server {
listen 80;
server_name mysite.net www.mysite.net;
root /mysite/site2;
# Load configuration files for the default server block.
#include /etc/nginx/default.d/*.conf;
charset utf-8;
index index.php index.html index.htm index.do;
location / {
root /mysite/site2;
}
}
此篇文章已被阅读1805 次