【问题标题】:Nginx configuration for multiple domain names with same root folder同一根文件夹下多个域名的 Nginx 配置
【发布时间】:2019-01-11 15:48:45
【问题描述】:

我在 AWS 上配置了 nginx 服务器请求流程,如下所示: 以下代码片段是 nginx 内部的自定义 conf 文件

server {
    listen   80;
    server_name  abc.tk www.abc.tk;

    # note that these lines are originally from the "location /" block
    #root   /usr/share/nginx/html;
    #index index.php index.html index.htm;

    #location / {
        #try_files $uri $uri/ =404;
    #}



    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

最初,我有一个域(如上),它工作正常,但现在我想对另一个域执行相同的重定向到同一个根文件夹。顺便说一句,我这里没有提到根文件夹,它来自 nginx.conf。我尝试了不同的方法来阅读在线资源,但没有取得任何成功。 以下是我现在正在尝试的,但它不起作用。给我 521:Web 服务器关闭错误。另外,我在这两个域中都使用了 cloudflare for ssl。

server {
    listen   80;
    server_name  abc.com www.abc.com abc.tk www.abc.tk;

    # note that these lines are originally from the "location /" block
    #root   /usr/share/nginx/html;
    #index index.php index.html index.htm;

    #location / {
        #try_files $uri $uri/ =404;
    #}



    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

快速帮助将不胜感激。提前致谢!

【问题讨论】:

  • 你想将用户重定向到一个特定的域还是仅仅为 server_name 中列出的所有域提供页面?
  • 是的......我想要后一个,为 server_name 中列出的所有域提供页面
  • 我也在服务器名称中尝试了正则表达式,如 abc.* www.abc.* 但结果相同

标签: nginx


【解决方案1】:

你可以试试这个方法吗:

server {
    listen   80;
    server_name  abc.com www.abc.com abc.tk www.abc.tk;

    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;
        location / {
            root /usr/share/nginx/html
            index  index.html index.htm index.php;
            try_files $uri $uri/;
        }


    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

【讨论】:

  • 让我试试,伙计
  • 是的....还是那个错误。我的 conf 文件中有几个位置块。我添加了你的并删除了我的。它打破了我现有的工作场景。我在两个文件夹中有两个角度项目。使用不同的服务器上下文,我正在运行那些
  • 你试过运行“/usr/nginx/sbin/nginx -t”看看它是否给你一些错误吗?如果服务器宕机,可能是某个 conf 文件有错误
  • 在server_name中添加新域名不起作用?
  • 是的,应该!我怀疑 SSL 证书有问题
猜你喜欢
  • 2017-05-30
  • 2012-10-14
  • 2020-01-19
  • 2020-02-11
  • 1970-01-01
  • 2011-03-15
  • 2012-03-03
  • 2022-11-23
  • 1970-01-01
相关资源
最近更新 更多