设为首页 收藏本站
查看: 952|回复: 0

[经验分享] nginx虚拟主机配置及负载均衡示例

[复制链接]

尚未签到

发表于 2016-12-28 10:09:09 | 显示全部楼层 |阅读模式
地址:http://wiki.codemongers.com/NginxChsFullExample

两个虚拟主机(纯静态-html 支持) - Two Virtual Hosts, Serving Static Files

http {
    server {
        listen          80;
        server_name     www.domain1.com;
        access_log      logs/domain1.access.log main;
        location / {
            index index.html;
            root  /var/www/domain1.com/htdocs;
        }
    }
    server {
        listen          80;
        server_name     www.domain2.com;
        access_log      logs/domain2.access.log main;
        location / {
            index index.html;
            root  /var/www/domain2.com/htdocs;
        }
    }
}

虚拟主机标准配置(简化) - A Default Catchall Virtual Host

http {
    server {
        listen          80 default;
        server_name     _ *;
        access_log      logs/default.access.log main;
        location / {
            index index.html;
            root  /var/www/default/htdocs;
        }
    }
}

在父文件夹中建立子文件夹以指向子域名 - Wildcard Subdomains in a Parent Folder

这是一个添加子域名(或是当DNS已指向服务器时添加一个新域名)的简单方法。需要注意的是,我已经将FCGI配置进该文件了。如果你只想使服务器为静态文件服务,可以直接将FCGI配置信息注释掉,然后将默认主页文件变成index.html。

这个简单的方法比起为每一个域名建立一个 vhost.conf 配置文件来讲,只需要在现有的配置文件中增加如下内容:

This is just a really easy way to keep adding new subdomains, or to add new domains automatically when DNS records are pointed at the server. Note that I have included FCGI here as well. If you want to just serve static files, strip out the FCGI config and change the default document to index.html. Rather than creating a new vhost.conf file for every domain, just create one of these:

server {
        # Replace this port with the right one for your requirements
        # 根据你的需求改变此端口
        listen       80;  #could also be 1.2.3.4:80 也可以是1.2.3.4:80的形式
        # Multiple hostnames seperated by spaces.  Replace these as well.
        # 多个主机名可以用空格隔开,当然这个信息也是需要按照你的需求而改变的。
        server_name  star.yourdomain.com *.yourdomain.com www.*.yourdomain.com;
        #Alternately: _ *
        #或者可以使用:_ * (具体内容参见本维基其他页面)
        root /PATH/TO/WEBROOT/$host;
        error_page  404              http://yourdomain.com/errors/404.html;
        access_log  logs/star.yourdomain.com.access.log;
        location / {
            root   /PATH/TO/WEBROOT/$host/;
            index  index.php;
        }
        # serve static files directly
        # 直接支持静态文件 (从配置上看来不是直接支持啊)
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|html)$ {
            access_log        off;
            expires           30d;
        }
        location ~ .php$ {
          # By all means use a different server for the fcgi processes if you need to
          # 如果需要,你可以为不同的FCGI进程设置不同的服务信息
          fastcgi_pass   127.0.0.1:YOURFCGIPORTHERE;
          fastcgi_index  index.php;
          fastcgi_param  SCRIPT_FILENAME  /PATH/TO/WEBROOT/$host/$fastcgi_script_name;
          fastcgi_param  QUERY_STRING     $query_string;
          fastcgi_param  REQUEST_METHOD   $request_method;
          fastcgi_param  CONTENT_TYPE     $content_type;
          fastcgi_param  CONTENT_LENGTH   $content_length;
          fastcgi_intercept_errors on;
        }
        location ~ /\.ht {
            deny  all;
        }
     }

地址:http://wiki.codemongers.com/NginxChsLoadBalanceExample

一个简单的负载均衡的示例,把www.domain.com均衡到本机不同的端口,也可以改为均衡到不同的地址上。

http {
    upstream myproject {
        server 127.0.0.1:8000 weight=3;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }

    server {
        listen 80;
        server_name www.domain.com;
        location / {
            proxy_pass http://myproject;
        }
    }
}

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.iyunv.com/thread-320541-1-1.html 上篇帖子: 详细解释:nginx中ChsHttpLogModule模块配置及各个参数含义 下篇帖子: nginx服务器 413 REQUEST ENTITY TOO LARGE
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表