西数超哥博客
运维经验教程分享

[原创]nginx如何配置多个站点使用或或配置虚拟主机

实际生产环境中,我们配置好了一个环境以后,肯定不是简简单单的单间一个站点就完事了,可能会搭建多个甚至上百个站点,尤其是一些IDC公司。之前我们开始安装nginx的时候,看到可以通过nginx.conf配置文件直接编辑,建立站点并绑定域名访问。但这种方式不是很方便,每次操作都要去编辑这个配置文件,而且一有报错启动不了,还耽误其他站点正常使用。下边我们说下配置多个站点的方法。
1,配置主配置文件。这个方法一带而过,基本不用这个方法。编辑nginx.conf,找到server以后复制这个大括号内容后重新开辟一个,修改里边的对应参数,比如绑定的域名、站点目录等。

   server {
        listen       80;
        server_name  www.21yunwei.com;
        #charset koi8-r;
        #access_log  logs/host.access.log  main;
        location / {
            root   /home/wwwroot/21yunwei;
            index  index.html index.htm index.php;
        }
        #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   html;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
         location ~ .php$ {
         root /home/wwwroot/21yunwei;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
         include fastcgi_params;
         }
    }

2,建立虚拟站点配置目录,里边单独存放站点配置文件。
首先在nginx/conf 目录下建立vhost目录,并修改配置文件nginx.conf让其加载这个目录:vi nginx.conf找到 #gzip  on; 在这行下边添加:
include vhost/*.conf;
保存退出。这样nginx以后就可以进去单独加载每个站点的独立配置文件。下边我们单独一一个站点为例,比如我们从vhost下创建了21yunwei.com.conf:

server {
        listen       80;
        server_name www.21yunwei.com 21yunwei.com;
        root /home/wwwroot/21yunwei;
        index  index.html index.php index.htm;
        error_page  400 /errpage/400.html;
        error_page  403 /errpage/403.html;
        error_page  404 /errpage/404.html;
       location ~ \.(php|php5)?$ {
       root           /home/wwwroot/21yunwei;
       fastcgi_pass   127.0.0.1:9000;
       fastcgi_index  index.php;
       fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
       include        fastcgi_params;
      }
}
~      

编辑以后保存,执行/home/nginx/sbin/nginx -s reload重启后生效。
如建立多个站点,可以通过/home/nginx/sbin/nginx -t查看站点配置情况

一些站点配置文件参数这里做一个简单说明:

server{}:  配置虚拟主机必须有这个段。
listen 80:   监听ip和端口,注意如仅有端口,表示当前服务器所有ip的80端口,如果只想某一个IP的80,写成listen x.x.x.x:80
server_name:站点绑定的域名,如果有多个,以空格隔开即可。
root /home/wwwroot/21yunwei;:站点根目录,存放网站源码的。尽量将目录名和网站域名相关,利于以后直观查看。
index  index.html index.php index.htm; 默认首页文件列表
location /{}  关于一些接口调用,具体请参考后边文章介绍。

转载请注明:西数超哥博客www.ysidc.top» [原创]nginx如何配置多个站点使用或或配置虚拟主机

https://www.ysidc.top 西数超哥博客,数据库,西数超哥,虚拟主机,域名注册,域名,云服务器,云主机,云建站,ysidc.top

赞(0)
声明:本站发布的内容(图片、视频和文字)以原创、转载和分享网络内容为主,若涉及侵权请及时告知,将会在第一时间删除。本站原创内容未经允许不得转载:西数超哥博客 » [原创]nginx如何配置多个站点使用或或配置虚拟主机