【问题标题】:ubuntu nginx vertual host issue not redirecting to desire folderubuntu nginx 虚拟主机问题未重定向到所需文件夹
【发布时间】:2017-09-15 05:28:25
【问题描述】:

我已向 ubuntu droplets 添加了 2 个域。 a.comb.com。两个 A 记录都重定向到 IP。

我还为这两个域创建了虚拟主机。喜欢

server {
        listen 80;
        listen [::]:80;

        root /var/www/html/b.com;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;

        server_name b.com www.b.com;

        location / {
                 try_files $uri $uri/ /index.php$is_args$args;
        }


        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

但是 b.com 没有重定向到期望文件夹。当我从 url 访问域时。它重定向到主 IP 文件夹,如 /var/www/html 而不是 /var/www/html/b.com

我的默认虚拟主机

server {
        listen 80;
        listen [::]:80;

        root /var/www/html/;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;

        server_name _;

        location / {
                 try_files $uri $uri/ /index.php$is_args$args;
        }


        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

我按照这些说明操作但仍然无法正常工作:https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-virtual-hosts-on-ubuntu-16-04

【问题讨论】:

    标签: php redirect nginx ubuntu-16.04


    【解决方案1】:
    server_name _;
    

    是一个包罗万象的服务器,如果它首先出现在您的配置中,它将首先被提供。您可以在此处阅读更多相关信息:http://nginx.org/en/docs/http/server_names.html

    如果你改成

    server_name a.com www.a.com;
    

    它应该适合你。 我建议你阅读这部分文档:http://nginx.org/en/docs/http/request_processing.html

    完整配置:

    server {
        listen 80;
    
        server_name b.com www.b.com;
    
        root /var/www/html/b.com/;
    
        # Add index.php to the list if you are using PHP
        index index.html index.htm index.php index.nginx-debian.html;
    
        location / {
                 try_files $uri /index.php?$args;
        }
    
    
        location ~ \.php$ {
                try_files $uri =404;
                include /etc/nginx/fastcgi_params;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                fastcgi_index   index.php;
    
                fastcgi_buffer_size 128k;
                fastcgi_buffers 256 16k;
                fastcgi_busy_buffers_size 256k;
                fastcgi_temp_file_write_size 256k;
        }
    }
    

    【讨论】:

    • 是的,它有效。但 b.com 和 www.b.com 不工作
    • 它也重定向到 a.com
    • 你重启你的 nginx 服务了吗?
    • 是的,我重新启动服务器一切正常,如 sudo nginx -t 和 sudo systemctl restart nginx。
    • 你用的是什么 nginx 版本?
    猜你喜欢
    • 2020-05-25
    • 1970-01-01
    • 2017-01-20
    • 2015-08-16
    • 2011-12-10
    • 2013-10-04
    • 2012-05-03
    • 2022-06-18
    • 2017-09-24
    相关资源
    最近更新 更多