linux Nginx设置多级域名

 本博文前提:

1. 有自己的云主机

2. 有独立的域名,如www.baidu.com

3. 使用的是linux,nginx(此处的是yum安装的)

一、创建子域名

在自己购买域名的地方设置,购买一个域名就可以开通很多个子域名,此处展示的是Ucloud处的,开通了一个test.yourdomain.com

 

二、配置Nginx

在linux中执行: vim /etc/nginx/conf.d/default.conf

原安装方式不同,路径会不一致

最重要的是:

server_name 不要使用 localhost,换成自己的域名www.yourdomain.com,其他的复制粘贴原来的service,修改一下server_name, root就能行了(当然,你得写进去的目录要存在有index.html)~

此处的环境是配置了thinkphp的URL重写,仅供参考,复制自己原来的修改就行。

server {
    listen       80; 
    server_name  test.yourDomain.cn;          #不要使用localhost!!!!

    location / { 
        root   /var/www/html;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
         }   
    }   

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }   

    location ~ .php$ {
        root           /var/www/html;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

server {
    listen       80;
    server_name  www.yourDomain.cn;
    location / {
        root   /var/www/tpblog/public;
        index  index.html index.htm index.php;
        if (!-e $request_filename) {
                rewrite  ^(.*)$  /index.php?s=$1  last;
                break;
         }
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ .php$ {
        root           /var/www/tpblog/public;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

}

三、重启nginx

service nginx restart

 嘿嘿,希望大家都能快速解决这个问题

原文地址:https://www.cnblogs.com/first-bloodlalala/p/12731024.html