优秀的编程知识分享平台

网站首页 > 技术文章 正文

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

nanyue 2025-01-21 20:22:49 技术文章 5 ℃

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服务器。

最近发表
标签列表