nginx服务器下配置多个站点绑定域名的方法二

文章作者:技术宅 | 2021-07-20
字体大小:

这里以配置2个站点(2个域名)为例,n 个站点可以相应增加调整,假设:


域名1 aaa.com 放在 /www/aaa

域名2 bbb.com 放在 /www/bbb


配置 nginx virtual hosting 的基本思路和步骤如下:


把2个站点 aaa.com, bbb.com 放到 nginx 可以访问的目录 /www/

给每个站点分别创建一个 nginx 配置文件 aaa.com.conf,bbb.com.conf, 并把配置文件放到 /etc/nginx/vhosts/

然后在 /etc/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)

重启 nginx  nginx启命令:/etc/init.d/nginx restar  如果重启失败可以直接重启服务器

服务器重启命令两种都可以:(linux重启时间短很快就可以完成了)

shutdown -r now  重启   

reboot   也表示重启!


下面是具体的配置过程:


1、在 /etc/nginx 下创建 vhosts 目录


mkdir /etc/nginx/vhosts

2、在 /etc/nginx/vhosts/ 里创建一个名字为 aaa.com.conf 的文件,把以下内容拷进去


server {

        listen  80;

        server_name  aaa.com www. aaa.com;


        access_log  /www/access_ aaa.log  main;


        location / {

            root   /www/aaa.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  /www/aaa.com/$fastcgi_script_name;

            include        fastcgi_params;

        }


        location ~ /\.ht {

            deny  all;

        }

}


3、在 /etc/nginx/vhosts/ 里创建一个名字为 bbb.com.conf 的文件,把以下内容拷进去


server {

        listen  80;

        server_name  bbb.com www. bbb.com;


        access_log  /www/access_ aaa.log  main;


        location / {

            root   /www/bbb.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  /www/bbb.com/$fastcgi_script_name;

            include        fastcgi_params;

        }


        location ~ /\.ht {

            deny  all;

        }

}

4、打开 /etc/nginix.conf 文件,在相应位置加入 include 把以上2个文件包含进来

在后面的位置

   # 包含所有的虚拟主机的配置文件

    include /usr/local/etc/nginx/vhosts/*;

}


5、重启 Nginx


/etc/init.d/nginx restart


注意:这个方式在lnmp 的编译环境下不好用的, 可以看下我发布的方法一

用那种方法不重要,重要的是实现了效果就可以的

http://www.ps288.com/wangluojishu/980.html


暂时关闭评论!
Copyright © 2019 技术宅 版权所有 关于我们| 法律声明| 免责声明| 广告服务| 联系我们| 投稿| 充值| 豫ICP备2023024979号