Nginx服务器设置多个站点使用多个配置文件【转】

Nginx能和其他的 web 服务器一样支持 virtual hosting,即一个IP对应多个域名以支持多站点访问,就像一个IP对应一个站点一样,所以是”虚拟”的。你想在一个 IP 下面放多少个站点就放多少,只要硬盘够大就行。

这里配置2个站点(2个域名)为例,n 个站点可以相应增加调整。

假设:
IP地址: 202.55.1.100
域名1 example1.com 放在 /www/example1
域名2 example2.com 放在 /www/example2

配置 nginx virtual hosting 的基本思路和步骤如下:
把2个站点 example1.com, example2.com 放到 nginx 可以访问的目录 /www/
给每个站点分别创建一个 nginx 配置文件 example1.com.conf,example2.com.conf,
并把配置文件放到 /etc/nginx/vhosts/
然后在 /etc/nginx.conf 里面加一句 include 把步骤2创建的配置文件全部包含进来(用 * 号)
重启 nginx

具体过程
下面是具体的配置过程:
1、在 /etc/nginx 下创建 vhosts 目录

mkdir /etc/nginx/vhosts

2、在 /etc/nginx/vhosts/ 里创建一个名字为 example1.com.conf 的文件,把以下内容拷进去

server {
        listen  80;
        server_name  example1.com www. example1.com;
        access_log  /www/access_ example1.log  main;
        location / {
            root   /www/example1.com;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/example1.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /\.ht {
            deny  all;
        }
}

3、在 /etc/nginx/vhosts/ 里创建一个名字为 example2.com.conf
的文件,把以下内容拷进去

server {
        listen  80;
        server_name  example2.com www. example2.com;
        access_log  /www/access_ example1.log  main;
        location / {
            root   /www/example2.com;
            index  index.php index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
       # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        location ~ \.php$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /www/example2.com/$fastcgi_script_name;
            include        fastcgi_params;
        }
        location ~ /\.ht {
            deny  all;
        }
}

4、打开 /etc/nginix.conf 文件,在相应位置加入 include 把以上2个文件包含进来

user  nginx;
worker_processes  1;
# main server error log
error_log       /var/log/nginx/error.log ;
pid     /var/run/nginx.pid;
events {
        worker_connections  1024;
}
# main server config
http {
        include       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"‘;
        sendfile        on;
        #tcp_nopush     on;
        #keepalive_timeout  0;
        keepalive_timeout  65;
        gzip  on;
        server {
                listen         80;
                server_name     _;
                access_log      /var/log/nginx/access.log main;
                server_name_in_redirect  off;
                location / {
                        root  /usr/share/nginx/html;
                        index index.html;
                }
        }
    # 包含所有的虚拟主机的配置文件
    include /usr/local/etc/nginx/vhosts/*;
}

5、重启 Nginx

/etc/init.d/nginx restart

转载地址:http://www.server110.com/nginx/201309/981.html

时间: 2024-08-07 19:22:17

Nginx服务器设置多个站点使用多个配置文件【转】的相关文章

nginx服务器设置path_info模式

1.find / -name nginx.conf找到nginx配置文件 2.找到locationlocation ~ \.php {#去掉这里的$ root /var/www/html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^(.+\.php)(.*)$; #增加这一句 fastcgi_param PATH_INFO $fastcgi_path_info; #增加这一句 fa

详解Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/53615313 上一篇分享了 Nginx + Tomcat 反向代理 负载均衡 集群 部署指南,感觉还是相当实用型的,但是一般集群部署是基于大访问量的,可能有的企业用不到,类似一些企业官网,访问量并不是很大,基于这个新需求,今天专门为大家分享一下 Nginx + Tomcat 反向代理 如何在一台服务器部署多个站点,节省服务器开支,就在这篇文章了. 首先我们需要安装好Nginx.j

Nginx + Tomcat 反向代理 如何在高效的在一台服务器部署多个站点

转载请注明出处:http://blog.csdn.net/smartbetter/article/details/53615313 上一篇分享了 Nginx + Tomcat 反向代理 负载均衡 集群 部署指南,感觉还是相当实用型的,但是一般集群部署是基于大访问量的,可能有的企业用不到,类似一些企业官网,访问量并不是很大,基于这个新需求,今天专门为大家分享一下 Nginx + Tomcat 反向代理 如何在一台服务器部署多个站点,节省服务器开支,就在这篇文章了. 首先我们需要安装好Nginx.j

PHP设置: nginx服务器伪静态怎么设置?

在nginx服务器上设置UWA伪静态,需修改nginx的配置文件. PHP设置部分: location ~ \.php { # 以下两行为避免,*.php文件不存在,而PHP-FPM(PHP FastCGI) 返回No input file specified错误,直接指向站点根目录 企业模板网站分享 在nginx服务器上设置UWA伪静态,需修改nginx的配置文件. PHP设置部分:location ~ \.php {    # 以下两行为避免,*.php文件不存在,而PHP-FPM(PHP

nginx服务器绑定域名和设置根目录的方法

nginx服务器绑定域名以及设置根目录非常方便,首先进入nginx安装目录,然后执行 vim conf/nginx.conf 打开nginx的配置文件,找到 server { ..... ..... } 这个代码段,这段代码就是用来配置对应站点的,首先我们应该在域名控制面板将域名解析到我们服务器的IP地址,然后绑定才可以生效 首先在我们的代码段中找到server_name这一项然后把后面的域名改成我们要绑定的域名即可 root这一项就是指定的根目录,设置成我们指定的目录即可 如果我们想绑定多个域

linux Nginx VirtualHost虚拟主机多站点设置

一台nginx服务器同一IP被注册多个不同域名,访问不同域名到该服务器后请求不同项目 本台nginx服务器的IP地址为 192.168.155.129 假设服务器有两个项目websuit_a,websuit_b,客户端访问websuit_a.com时请求websuit_a项目,访问websuit_b.com时请求websuit_b项目 首先在项目根目录下新建项目文件夹websuit_a和websuit_b存放两个虚拟站点的程序 mkdir -m777 -p /usr/local/nginx/ht

修改Linux内核参数提高Nginx服务器并发性能

当linux下Nginx达到并发数很高,TCP TIME_WAIT套接字数量经常达到两.三万,这样服务器很容易被拖死.事实上,我们可以简单的通过修改Linux内核参数,可以减少Nginx服务器 的TIME_WAIT套接字数量,进而提高Nginx服务器并发性能.   vi /etc/sysctl.conf   增加以下几行: net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_syncookie

centos上安装nginx服务器实现虚拟主机和域名重定向

Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的Rambler.ru站点开发的,第一个公开版本0.1.0发布于2004年10月4日.其将源代码以类BSD许可证的形式发布,因它的稳定性.丰富的功能集.示例配置文件和低系统资源的消耗而闻名.2011年6月1日,nginx 1.0.4发布. Nginx是一款轻量级的Web 服务器/反向代理服务器及电子邮件(IMAP

一台nginx服务器多域名配置

Nginx强大的正则表达式支持,可以使server_name的配置变得很灵活,如果你要做多用户博客,那么每个用户拥有自己的二级域名也就很容易实现了. 下面我就来说说server_name的使用吧: server_name的匹配顺序 Nginx中的server_name指令主要用于配置基于名称虚拟主机,server_name指令在接到请求后的匹配顺序分别为: 1.准确的server_name匹配,例如: server { listen 80; server_name ssdr.info www.s