nginx服务器如何配置多虚拟站点及其它

nginx服务器如何配置多虚拟站点,操作如下:

1、配置文件目录:除了nginx服务器根目录下的nginx.conf文件外,其它多站点一般配置在目录"conf/vhost"下,演示如下:

目录图:

目录列表图:

站点配置文件以“.conf”为后缀,名称一般按域名来起就可以了,多站点创建不同的.conf文件。

2、配置站点,代码如下:

server
    {
        #listen       80;
        listen 443;//端口
        server_name 你的域名;
        index index.html index.php admincp.php;
        root  /mnt2/qqck;
        #ssl证书开始
        ssl on;
        ssl_certificate   /usr/local/nginx/conf/cert/138.com.pem;
        ssl_certificate_key  /usr/local/nginx/conf/cert/138.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;
        #ssl证书结束
        

        //配置站点的一起规则
        location /
            {

                index index.php index.html index.htm admincp.php;
                if (!-e $request_filename) {
                  rewrite ^/admincp.php/(.*)$ /admincp.php?$1 last;
                  rewrite ^(/index.php|/index.php/|/!admincp.php|/)(.*)$ /index.php?$1 last;
                  break;
                }
            }

        location ~ .*\.(php|php5)?$
            {
                try_files $uri =404;
                fastcgi_pass  unix:/tmp/php-cgi.sock;
                fastcgi_index index.php;
                include fastcgi.conf;
            }

        location /status {
            stub_status on;
            access_log   off;
        }

        location ~ .*\.(gif|jpg|a|jpeg|png|bmp|swf|mp3)$
            {
                expires      30d;
                #add_header wall  "hey!guys!give me a star.";
            }

        location ~ .*\.(js|css)?$
            {
                expires      12h;
                #add_header wall  "hey!guys!give me a star.";
            }

        access_log  /home/wwwlogs/access.log;
    }

演示效果如下:

注意站点是从server开始的,其它跟nginx.conf的配置是一样的,演示的是https的,http的去掉证书块的代码,监听端口修改为80.

3、监听多域名跳转到指定域名,配置如下:

代码如下:

server
        {
                #listen       80;
                listen 443;
                server_name www.test.club test.club;
                #指定跳转开始
                if ($host != 'www.test.club') {
                    rewrite ^/(.*)$ https://www.test.club/$1 permanent;
                }
                #指定跳转结束
                index index.html index.php admincp.php;
                root  /mnt/test;
                ssl on;
                ssl_certificate   /usr/local/nginx/conf/cert/107.club.pem;
                ssl_certificate_key  /usr/local/nginx/conf/cert/107.club.key;
                ssl_session_timeout 5m;
                ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
                ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
                ssl_prefer_server_ciphers on;

                location /
                        {

演示效果如下:

这样不同域名都可以打开同一个站点了,但最终显示的是同一个域名了,关键代码为:

if ($host != 'www.test.club') {
   rewrite ^/(.*)$ https://www.test.club/$1 permanent;
}

注:配置好后,请重启 nginx服务器。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

行思理

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值