【问题标题】:<Nginx> How to setup Multi dynamic virtual hosts + www + subdomain<Nginx> 如何设置多动态虚拟主机+www+子域
【发布时间】:2014-09-11 04:05:48
【问题描述】:

关于nginx虚拟主机和子域配置的一些问题, 但它无法实现。

我有 3 个域和一个 IP 地址(多站点)

www.a1.com
www.a2.com
www.a3.com

我想用一个简单的nginx配置来设置,当我进入

网址重写

a1.com      => force redirect non-www to WWW url www.a1.com

www.a1.com      => pass to /usr/share/nginx/html/a1.com/
www.blog.a1.com => pass to /usr/share/nginx/html/a1.com/blog/
*www.photo*.a1.com=> pass to /usr/share/nginx/html/a1.com/*photo*/
...and so on

a2.com      => force redirect non-www to WWW url www.a2.com

www.a2.com      => pass to /usr/share/nginx/html/a2.com/
www.blog.a2.com => pass to /usr/share/nginx/html/a2.com/blog/
www.photo.a2.com=> pass to /usr/share/nginx/html/a2.com/photo/
www.user.a2.com=> pass to /usr/share/nginx/html/a2.com/user/
...and so on

a1、a2 和 a3.com 使用相同的配置。

这是我的第一个代码,我该如何解决?

伪代码

if subdomain lacks 'www' then pass to $scheme://www.$host$request_uri;
if subdmain <>'' then pass to /usr/share/nginx/html/$host/<#subdmain#>/$request_uri;

/etc/nginx/conf.d/default.conf

server {
    # Redirect non-www to WWW
    server_name "~^(?!www\.).*" ;
    return 301 $scheme://www.$host$request_uri;
}

server {

    listen 80 default;

    #automatic judging hostname
    server_name  ~^(www\.)?(?<domain>.+)$;

    location / {

        #automatic change folder
        root    /usr/share/nginx/html/$domain/;

        index index.html index.php;
        #try_files $uri $uri/ /index.php?$query_string;
    }

    # 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;

        # judging subdomain which have "www"
        if ($subdomain = 'www') {
            fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$domain$fastcgi_script_name;
        }
        # judging subdomain like "blog" and trans to blog folder
        else {
            fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html/$domain/$subdomain/$fastcgi_script_name;
        }
        include    fastcgi_params;
    }
}

【问题讨论】:

    标签: nginx php php-5.5 winginx


    【解决方案1】:

    不要过度复杂化 nginx 配置 :) 我想你可以创建 3 个 server {} 块。 第一,就像你现在一样。 www.domain.com 第 2 名,www.subdomain.domain.com 第 3 名

    对于第二个服务器定义,您需要这个正则表达式:

    server_name ~^(www\.)(?P<domain>.+)\.com$;
    root /usr/share/nginx/html/$domain.com/www;
    

    对于第三个服务器定义,您需要这个正则表达式:

    server_name ~^(www\.)(?P<subdomain>.+)\.(?P<domain>.+)\.com$;
    root /usr/share/nginx/html/$domain.com/$subdomain;
    

    在使用正则表达式并处理非 .com 域后,您可以改进这些规则:)

    【讨论】:

      猜你喜欢
      • 2011-02-22
      • 1970-01-01
      • 2012-12-24
      • 2012-01-02
      • 2013-10-13
      • 2013-06-11
      • 2020-05-08
      • 1970-01-01
      • 2021-05-09
      相关资源
      最近更新 更多