【问题标题】:Nginx location to override a Laravel 5.2 route?Nginx 位置覆盖 Laravel 5.2 路由?
【发布时间】:2017-02-15 22:25:49
【问题描述】:

我有一个 nginx Web 服务器,在端口 80 上为 Laravel 5.2 应用程序提供服务。

通过访问 mydomain.com,我的 laravel 应用程序启动,一切都按预期工作。

另一方面,我有一个 nodejs 应用程序在端口 83 上运行,它提供其他类型的内容。 当我想通过主域上的反向代理提供我的 nodejs 内容时,问题就来了。

我要做的是让 nginx 在 domain.com/api/info/socket 我的 nodejs 应用程序上服务,而 laravel 不会尝试使用它的路由系统解析该 url。这是我尝试这样做的 nginx 配置:

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

        #access_log /var/log/nginx/access_log combined;
        #index index.php index.html index.htm index.nginx-debian.html
        return 301 https://$server_name$request_uri;
    }

    server {

        # SSL configuration

        listen 443 ssl default_server;
        listen [::]:443 ssl default_server;
        include snippets/ssl-mydomain.com.conf;
        include snippets/ssl-params.conf;

        access_log /var/log/nginx/access_log combined;
        index index.php index.html index.htm index.nginx-debian.html

        server_name mydomain.com www.mydomain.com;

        location / {
            root /var/www/mydomain.com/public;
            try_files $uri $uri/ /index.php?$query_string;
        }

        location /stream {
            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-Proto $scheme;

            proxy_read_timeout      300;
            proxy_pass              http://localhost:9999/stream;
        }

        # This will not let laravel parse this url. replace index.html if you have some other entry point.
        location /api/info/socket {
            try_files $uri $uri/ /api/info/socket/index.html;
        }

        location = /api/info/socket {
            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-Proto $scheme;

            proxy_read_timeout      300;
            proxy_pass              http://localhost:83;
        }

        location ~ \.php$ {
            set $php_root /var/www/mydomain.com/public;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~ /\.ht {
            deny all;
        }

        location ~ /.well-known {
        allow all;
        }
    }

但每次我访问该 URL 时,我都会收到 laravel 404 错误消息,因为它不是我的路由配置的一部分。关于如何在不让 Laravel 接管的情况下强制 nginx 提供特定 URL 的任何想法?

【问题讨论】:

  • 您是否有权为子域创建 dns 条目?可以为您简化。
  • 这当然有帮助,但如果可能的话,我想将所有公共端点保留在 domain.com/api 下
  • 可以使用api.domain.com 转发主域上的请求。不是proxy_set_header Host $http_host; 吗?你在收听localhost127.0.0.1 吗?
  • 另外,您可能需要使用proxy_pass $scheme://localhost:83;,这样您就不会将所有流量重定向到 https 之外
  • @Blake 我通过从 http 重定向到 https 来强制执行 https,那么 $scheme 还需要吗?

标签: php node.js laravel nginx server


【解决方案1】:

一切都很好。您只需添加几行即可。

server_name domain.com www.domain.com;    
index index.php index.html index.htm index.nginx-debian.html

location / {
root /var/www/domain.com/public;
     try_files $uri $uri/ /index.php?$query_string;
}

# This will not let laravel parse this url. replace index.html if you have some other entry point.
#location /api/info/socket {
#    try_files $uri $uri/ /api/info/socket/index.html;
#}

# If above doesn't work try this.
location /api/info/socket/ {
     try_files $uri $uri/ @api;
}
location @api {
     rewrite /api/info/socket/ /api/info/socket/index.html;
}

location = /api/info/socket {
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-Proto $scheme;

proxy_read_timeout      300;
proxy_pass              http://localhost:83;
}

# This is the php processing needed for laravel
location ~ \.php$ {
#include snippets/fastcgi-php.conf;
set $php_root /var/www/mydomain.com/public;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
include fastcgi_params;
}

另外,你需要把你的node应用放到公共目录,别忘了重启nginxsudo service nginx restart
让我知道它是否解决了您的问题。 :)

【讨论】:

  • 我做了这个更改,但我仍然遇到同样的问题。如果我发布我的整个默认配置而不仅仅是相关的服务器块会更好吗?
  • 我刚刚用附加信息更新了主要问题。我可以指出 /stream 没有被 laravel 解析并按预期工作,这真的很奇怪。我也在用 sudo systemctl restart nginx 重新启动,不知道它是否改变了什么。 Ubuntu 16.04
  • 好的,告诉我你想成为什么样的网址? mydomain.com/apimydomain.com/api/info/socket ?以及您要访问的网址是什么?
  • 现在我的 Laravel 应用解析了几个 url,例如:mydomain.com/api/info/poll,所以我想在没有 laravel 干扰的情况下访问的新 url 是 mydomain.com/api/info/socket。现在我正在尝试访问该 url,我从 Laravel 获得了默认的 404。
  • /api/info/socket 上的入口文件是什么? index.html 还是别的什么?
【解决方案2】:

尝试将location /api/info/socket块移到location /上方

【讨论】:

  • 刚刚尝试过,我遇到了完全相同的问题,Laravel 的 404 页面。我还通过添加 = 来修改位置块,因为文档建议完全匹配,但它也不起作用。
  • try_files $uri $uri/ /index.php$is_args$args;替换try_files $uri $uri/ /index.php?$query_string;
  • 不幸的是,这也不能解决问题:(
  • 感谢您帮助我 :)
猜你喜欢
  • 2016-09-30
  • 2015-04-23
  • 2016-09-21
  • 1970-01-01
  • 2020-02-03
  • 2020-05-02
  • 2015-01-12
  • 1970-01-01
  • 2013-08-06
相关资源
最近更新 更多