请教 nginx 多个 server 怎么配置~ - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ren2881971
V2EX    NGINX

请教 nginx 多个 server 怎么配置~

  •  
  •   ren2881971
    ren2881971 2015-04-02 21:13:24 +08:00 11444 次点击
    这是一个创建于 3898 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前想在nignx配置两个server
    其中已经有一个配置了域名正在用。

    另一个我可以把server name 配置成 xx.xxx.xx.xx/test
    然后我通过 ip/test 访问么?

    就是多个server 但只有一个域名 另外一个server 想通过ip地址+路径访问。

    就像tomcat 里多个应用 直接ip/应用名 就能访问~~

    25 条回复    2015-04-03 11:32:58 +08:00
    Livid
        1
    Livid  
    MOD
    PRO
       2015-04-02 21:18:49 +08:00 via iPhone
    /test 这个部分是通过 location 来配置的。

    Nginx 配置的结构是 http -> server -> location
    ren2881971
        2
    ren2881971  
    OP
       2015-04-02 21:21:48 +08:00
    @Livid 额。。 我是在location里配置的啊。
    假如主机ip 是 123.4.5.6 同时我有一个域名www.xx.com并且有一个应用在这绑定这个域名运行中。
    我可以把应用配置成 123.4.5.6/test 这种方式来访问么?
    还是说必须配置成 www.xx.com/test 通过域名的方式来进行访问?
    ren2881971
        3
    ren2881971  
    OP
       2015-04-02 21:27:08 +08:00
    @ren2881971 补充 把另一个应用配置成123.4.5.6/test 方式来访问
    huson
        4
    huson  
       2015-04-02 21:50:24 +08:00
    在location /test 里面用 proxy_pass 反代到你的应用
    ren2881971
        5
    ren2881971  
    OP
       2015-04-02 22:09:04 +08:00
    @huson server {
    listen 80;
    server_name 123.56.118.252;
    index index.html index.htm index.php;
    root /alidata/www/weixin;
    location ~ .*\.(php|php5)?$
    {
    #fastcgi_pass unix:/tmp/php-cgi.sock;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    expires 30d;
    }
    location ~ .*\.(js|css)?$
    {
    expires 1h;
    }
    location /test{
    proxy_pass http://123.56.118.252/;
    }

    貌似location 配置的不对~~ 求教
    zhujunsan
        6
    zhujunsan  
       2015-04-02 22:15:21 +08:00
    livid亲自来回复啦@@!

    location = /test{
    proxy_pass http://123.56.118.252/;
    }

    location后面可以跟正则表达式,官方文档: http://nginx.org/en/docs/http/ngx_http_core_module.html#location
    ren2881971
        7
    ren2881971  
    OP
       2015-04-02 22:19:24 +08:00
    @zhujunsan 额。。。。请求 *.php 还是会报403~~ location 还需要增加 php的匹配配置么。。
    zhujunsan
        8
    zhujunsan  
       2015-04-02 22:30:16 +08:00
    @ren2881971 *.php被上面那条location吃掉了吧 XD
    ren2881971
        9
    ren2881971  
    OP
       2015-04-02 22:39:59 +08:00
    @zhujunsan 哎。 悲剧 location 配置不明白 只能改端口了~~

    很好奇。一般这种场景都怎么配置的。
    nginx 下两个server 一个绑定域名。 另一个没有域名的话该怎么访问呢?
    myself
        10
    myself  
       2015-04-02 22:48:20 +08:00
    网站速度好快
    zhujunsan
        11
    zhujunsan  
       2015-04-02 22:48:41 +08:00
    @ren2881971 2个不一样的二级域名。foo.xxx.com, bar.xxx.com这样分。不止两个server,几十个server也是正常,每个server写在一个配置文件里。
    zhujunsan
        12
    zhujunsan  
       2015-04-02 22:57:10 +08:00
    @ren2881971
    这么写吧:
    location ^~ /test{
    proxy_pass http://123.56.118.252/;
    }
    并且把这个规则放到那个php的上面
    ren2881971
        13
    ren2881971  
    OP
       2015-04-02 22:59:26 +08:00
    @zhujunsan nginx 配置两个二级域名 那在万网哪里 我怎么配置。。只有一个ip啊。
    zhujunsan
        14
    zhujunsan  
       2015-04-02 23:03:32 +08:00
    @ren2881971 DNS管理里面,设置两条A记录就好了。不过国内的话还是建议把DNS解析放到dnspod吧~免费稳定也很好用~
    ren2881971
        15
    ren2881971  
    OP
       2015-04-02 23:04:43 +08:00
    难道nginx 就不能像tomcat 那样 自己定义url 匹配 应用么?
    感觉反向代理是兜了个圈子啊。
    ren2881971
        16
    ren2881971  
    OP
       2015-04-02 23:07:55 +08:00
    @zhujunsan 额。。 我理解下哈 万网哪里两个二级域名都对应一个主机ip 然后在nginx里面 对不同的二级域名进行目录匹配是么?
    zhujunsan
        17
    zhujunsan  
       2015-04-02 23:48:59 +08:00 via Android
    @ren2881971 嗯就是这样的
    Busy
        18
    Busy  
       2015-04-02 23:49:30 +08:00
    @ren2881971 你不觉得 nginx 跟 php-fpm 的通讯,就是一个反向代理的模型么?区别是有些监听端口,有些监听 socket,有些则监听 path
    kevin8096
        19
    kevin8096  
       2015-04-02 23:49:37 +08:00
    location /test {
    index index.php;
    root /xxx
    if (!-f $request_filename) {
    rewrite ^(.*)$ /index.php last;
    break;
    }
    }
    location ~* \.php {
    root /xxx
    fastcgi_split_path_info ^(.+\.php)(.*)$;
    fastcgi_pass 127.0.0.1:9999;
    include fastcgi.conf;
    }
    ren2881971
        20
    ren2881971  
    OP
       2015-04-02 23:52:03 +08:00
    @Havee 太高深 听不懂 慢慢学吧。 反向代理在我这还是个新鲜名词~~~
    ren2881971
        21
    ren2881971  
    OP
       2015-04-02 23:52:43 +08:00
    @kevin8096 3q !!!!
    SharkIng
        22
    SharkIng  
       2015-04-03 06:09:27 +08:00
    Livid居然换头像了....

    location指定好路径,不指定主机名,就可以通过IP访问

    但是需要确定下没有其他的Nginx配置,一般装Nginx的时候都会有一个default的配置指定了其他的路径的
    ren2881971
        23
    ren2881971  
    OP
       2015-04-03 08:59:21 +08:00
    @SharkIng 好的 目前已经解决3q!
    xiaobu
        24
    xiaobu  
       2015-04-03 09:43:29 +08:00
    你访问IP,看访问的哪个目录,然后直接那个目录下面放test文件夹就行了
    oamzn
        25
    oamzn  
       2015-04-03 11:32:58 +08:00
    网站访问速度好快,求服务器配置。
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5543 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 40ms UTC 06:06 PVG 14:06 LAX 22:06 JFK 01:06
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86