Nginx 配置多站点vhost

摘要:
server_name与root保持一致即目录和域名一一对应!!!-e$request_filename){  # rewrite可根据网站需要增删rewrite^/(.*)/index.phplast;}location/{indexindex.phpindex.htmlindex.htm;}#error_page404/404.html;#redirectservererrorpagestothestaticpage/50x.html#error_page500502503504/50x.html;location=/50x.html{root/var/www/testsite1/;}#proxythePHPscriptstoApachelisteningon127.0.0.1:80##location~.php${#proxy_passhttp://127.0.0.1;#}#passthePHPscriptstoFastCGIserverlisteningon127.0.0.1:9000#location~*.php${fastcgi_indexindex.php;fastcgi_pass127.0.0.1:9000;includefastcgi_params;fastcgi_paramSCRIPT_FILENAME$document_root$fastcgi_script_name;fastcgi_paramSCRIPT_NAME$fastcgi_script_name;}#denyaccessto.htaccessfiles,ifApache'sdocumentroot#concurswithnginx'sone##location~/.ht{#denyall;#}}5、重启Nginxsystemctlrestartnginx6、编辑/etc/hosts  !!!

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

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

/var/www/testsite1
/var/www/testsite2

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/testsite1.conf
/etc/nginx/conf.d/testsite2.conf

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

复制代码
server {
    listen       80;
    server_name   www.test.com;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;
    root   /var/www/testsite1/;
    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/testsite1/;
    }

    # 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.testsite1.com
127.0.0.1        www.testsite2.com

7、设置成功

免责声明:文章转载自《Nginx 配置多站点vhost》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇如何卸载VSTO外接程序?gridControl通过设置条件,改变单元格颜色下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JS动态创建表格比较【转】

目标:生成一个2000*5的表格,每个单元格的内容是行号+逗号+列号 方法一:使用createElement生成表格,使用insertRow和insertCell方法生成行列,单元格的内容使用innerHTML属性进行填充。 方法二:使用createElement生成表格,使用CreateElement方法生成行列,单元格的内容使用了createTex...

.net中RSA加密解密

1、产生密钥: private static void CreateKey() { using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider()) { string public...

nginx 部署web页面问题

nginx 部署web页面的时候,路径都是对的,但是css文件就是不起作用,控制台提示如下,原来是格式的问题,截图如下: css 被转成了application/octet-stream,这个是nginx的默认格式,所以需要在nginx的配置文件上配置type,配置文件截图为: mime.types 内容为: types { text/html...

javascript版万年历

1 <!DOCTYPE html PUBLIC "-//W3C//Dtd XHTML 1.0 transitional//EN" "http://www.w3.org/tr/xhtml1/Dtd/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"...

Nginx报错:Sorry, the page you are looking for is currently unavailable. Please try again later.

查看了进程, nginx, php-fpm都在运行, 排除程序错误, 那么就是配置的问题了. 一个可能的错误, 是由于配置中的fastcgi_pass 配置错了 错误的配置如下 server { listen 80; server_name localhost; #charset koi8-r; #a...

nginx配置一、二级域名、多域名对应(api接口、前端网站、后台管理网站)

前提:安装好nginx,如果已经启动nginx,先停止,命令: ./usr/local/nginx/sbin/nginx -s stop 修改nginx配置 vi /usr/local/nginx/conf/nginx.conf 配置好以后的nginx.conf文件内容: #user nobody; worker_processes 1;...