Nginx虚拟主机(Virtual Host)配置

2017-09-30  本文已影响0人  NoFacePeace

概念

虚拟主机(Virtual Host)可以在一台服务器上绑定多个域名,架设多个不同的网站,一般在开发机或者要部署多个小网站的服务器上需要配置虚拟主机。nginx的虚拟主机配置其实也挺简单,为了使得配置文件清晰,可以改每一个虚拟主机建立一个配置文件,然后在主配置文件(nginx.conf)里使用include语句包含所有的虚拟主机配置文件。

操作

sudo mkdir /usr/etc/nginx/vhosts
sudo vim /usr/etc/nginx/vhosts/domain1.com.conf
server {
    listen 80;
    server_name domain1.com www.domain1.com;
    access_log /var/log/access_domain1.log main;
    location / {
        root /var/www/domain1.com;
        index index.php index.html index.htm;
    }
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /var/www/domain1.com/$fastcgi_script_name;
        include fastcgi_params;
    }
    location ~ /\.ht {
        deny all;
    }
}
sudo vim /usr/local/etc/nginx/nginx.conf

#在 http 配置节的结束花括号 } 前一行加入如下语句
include /usr/local/etc/nginx/vhosts/*;
nginx -s reload
上一篇 下一篇

猜你喜欢

热点阅读