【问题标题】:Laravel 6 behind proxy always return 404代理后面的 Laravel 6 总是返回 404
【发布时间】:2020-02-25 01:44:10
【问题描述】:

我有一台运行 wordpress 的 nginx 服务器,地址为 https://example.com(我称之为服务器 A) 然后,我想在另一个 nginx 服务器上托管 laravel 应用程序 https://example.com/laravel(I'll call this Server B) 这个服务器B配置了https://laravel.example.com

在服务器 A 中,我有以下配置。

location /laravel/ {
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
 proxy_set_header X-Forwarded-Host $host;
 proxy_pass https://subdomain.example.com;
}

Server B 中的配置如下。

server {
    root /var/www/Saskatchewan/public;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name subdomain.example.com;

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

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

    location ~ /\.ht {
        deny all;
    }

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/subdomain.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/subdomain.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    access_log      /var/www/example.com/access.log;
    error_log       /var/www/example.com/error.log;
}
server {
    if ($host = learning.tieng-viet.jp) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80;

    server_name subdomain.example.com;
    return 404; # managed by Certbot


}

问题是当我访问 laravel 支持的站点时(https://example.com/laravel), 它返回 404(不是 nginx,而是 Laravel 404)。 我也尝试访问一些静态文件,但结果相同。

我能够确认请求确实到达了服务器 B。 而404页面是由Laravel生成的。

当我使用以下内容访问该网站时,它工作得非常好。 https://subdomain.example.com

任何想法将不胜感激。 谢谢

更新 也不提供静态文件。它返回 404 Laravel not found 错误页面。

【问题讨论】:

  • 返回404laravel默认错误页面还是Nginx错误页面?
  • @user8555937 Laravel 404 页面

标签: laravel nginx proxy


【解决方案1】:

首先,在.env 中配置您的网站默认地址。这样做后清除应用程序缓存:php artisan config:cache

然后使用路由来允许不同域的访问:

// by domain
Route::domain('example.com')->group(function(){
    Route::get('/test', function(){
        return response('another domain');
    });
});

// relative to default APP_URL
Route::get('/test', function(){
    return response('default APP_URL');
});

如果您想为更多域复制路由,请使用以下概念:

$routes = function() {
    Route::get('/test', function(){
        return response('This route is duplicated for both domains');
    });
}

Route::domain('example.com')->group($routes);
Route::domain('laravel.example.com')->group($routes);

希望这会有所帮助。


顺便说一句,确保您的 Nginx 代理向 laravel 服务器输出正确的 HTTP_* 标头,如 $_SERVER。可以在这里找到他们的完整列表https://www.php.net/manual/en/reserved.variables.server.php

【讨论】:

【解决方案2】:

问题可能是服务器 A 中 location /laravel/ 语句的顺序。确保在 nginx 配置中没有其他具有更通用匹配器的 location 规则在此之前。

不好:

server {
  listen 80;
  server_name example.com;

  location ~ /(.*) {
    ...
  }

  location /laravel/ {
    ...
  }
}

好:

server {
  listen 80;
  server_name example.com;

  location /laravel/ {
    ...
  }

  location ~ /(.*) {
    ...
  }
}

更多详情,请查看how an nginx request is being processed

顺便说一句,不知道你为什么将服务器 A 的 /laravel/ 设置为反向代理而不是永久重定向。在可用性方面,我认为这会更好。

【讨论】:

    猜你喜欢
    • 2015-07-31
    • 2019-03-12
    • 2021-05-16
    • 2021-05-04
    • 2021-08-05
    • 2020-11-11
    • 1970-01-01
    • 2013-09-02
    • 1970-01-01
    相关资源
    最近更新 更多