码迷,mamicode.com
首页 > 其他好文 > 详细

Nginx 配置多站点vhost

时间:2019-09-03 16:31:55      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:use   网站   fast   ref   domain   htaccess   http   步骤   href   

假设你想在Linux Nginx中用不同的域名访问不同的目录,这时就要配置多个vhost,具体配置如下,假设网站根目录设定在/var/www/

 

1、在/var/www/下新建两个目录

/var/www/ushark.net
/var/www/ushark.wang

 

2、编辑/etc/nginx/nginx.conf

技术图片
http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘$remote_addr - $remote_user [$time_local] "$request" ‘
                      ‘$status $body_bytes_sent "$http_referer" ‘
                      ‘"$http_user_agent" "$http_x_forwarded_for"‘;

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;   #主要是加入此行,如有则忽略
}
技术图片

 

3、在/etc/nginx/conf.d下新建两个conf文件,

/etc/nginx/conf.d/ushark.net.conf
/etc/nginx/conf.d/ushark.wang.conf

 

4、复制如下配置信息到两个文件中,只要修改红色部分内容  !!! server_name与root保持一致即目录和域名一一对应 !!!

技术图片
server {
    listen       80;
    server_name   www.ushark.net;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root   /var/www/ushark.net/;
    if (!-e $request_filename){    # rewrite可根据网站需要增删
            rewrite ^/(.*) /index.php last;  
    }  

    location / {
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/ushark.net/;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~* \.php$ {
        fastcgi_index   index.php;
        fastcgi_pass    127.0.0.1:9000;
        include           fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }

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

 

5、重启Nginx

systemctl restart nginx

 

6、 编辑/etc/hosts  !!! 核心步骤 !!!

[root@bogon ~]# vi 127.0.0.1        localhost.localdomain localhost
::1              localhost6.localdomain6 localhost6
127.0.0.1        www.ushark.net
127.0.0.1        www.ushark.wang

 

7、设置成功

Nginx 配置多站点vhost

标签:use   网站   fast   ref   domain   htaccess   http   步骤   href   

原文地址:https://www.cnblogs.com/sea520/p/11453581.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!