Nginx配置具有相同根文件夹的多个域名

时间:2019-01-11 15:48:45

标签: nginx

我已经在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用于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;
    }
}

快速帮助将不胜感激。预先感谢!

1 个答案:

答案 0 :(得分:0)

您可以这样尝试吗?

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