ngnix 配置域名和二级域名

#超级管理人权限

sudo su

#配置本地域名

nano /etc/hosts

#添加多个域名到hosts 本件中


127.0.0.1       test.com
127.0.0.1       www.test.com
127.0.0.1       yascmf.test.com
127.0.0.1       blog.test.com 

法一:

default  配置参照 ubuntu 安装 lnmp 环境


#修改sites-available/default 文件
nano /etc/nginx/sites-available/default 


#复制原 service 在后面添加

server {
        listen 80;
        listen [::]:80;

        server_name blog.test.com;

        root /usr/share/nginx/html/blog;
        index index.php index.html index.htm index.nginx-debian.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /html;
        }

        error_page 404 /404.html;

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

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_nam$
                include fastcgi_params;
        }

       # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
                deny all;
        }
}

修改service_name 和root 成相应的路径

# 重起 nginx

service nginx restart

恭喜你,配置成功!


法二

# 或则复制default 成 blog.test.com

cp /etc/nginx/sites-available/default  /etc/nginx/sites-available/blog.test.com

编辑blog.test.com

# 添加如上service

server {
        listen 80;
        listen [::]:80;

        server_name blog.test.com;

        root /usr/share/nginx/html/blog;
        index index.php index.html index.htm index.nginx-debian.html;
	
	location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                # try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php;
        }

        error_page 404 /404.html;

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

        location ~ \.php$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        location ~ /\.ht {
        	deny all;
        }
}

修改相应的service_name 和 root


# 在sites-enabled 添加链接指向

sudo ln -s /etc/nginx/sites-available/blog.test.com /etc/nginx/sites-enabled/blog.test.com

# 重起 nginx

service nginx restart

参照:

albertogrespan.com/blog/running-multiple-domains-or-subdomains-in-nginx-with-server-blocks/

yanue.net/post-157.html


ref:nginx 配置说明

www.cnblogs.com/xiaogangqq123/archive/2011/03/02/1969006.html

www.cnblogs.com/xiaogangqq123/archive/2011/03/04/1971002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值