简体   繁体   English

Laravel-所有路由上的NGINX + Apache反向代理404

[英]Laravel - NGINX + Apache reverse proxy 404 on all routes

i'm setting up a project in Laravel which makes use of a custom vagrant box based on Ubuntu 14.04 and which runs NGINX and Apache in a typical reverse proxy setup. 我正在Laravel中建立一个项目,该项目利用基于Ubuntu 14.04的自定义无用信息框,并在典型的反向代理设置中运行NGINX和Apache。

Now, the issue is that something along the way is not registering the URL path, so when i enter http://mydomain.test/hello what i ultimately get is just a http://mydomain.test/index.php on the other end and it seems to completely forget about the route param. 现在,问题在于,一路走来并没有注册URL路径,所以当我输入http://mydomain.test/hello ,我最终得到的只是在http://mydomain.test/index.php上。另一端,似乎完全忘记了路线参数。

I'm using Apache 2.4.7 with Mod PHP and the Apache config is as follows. 我将Apache 2.4.7与Mod PHP配合使用,并且Apache配置如下。

<VirtualHost 127.0.0.1:8080>
    ServerName application.test
    ServerAlias www.application.test
    DocumentRoot "/home/vagrant/application/public"

    ErrorLog /var/log/apache2/error.log
    CustomLog /var/log/apache2/access.log combined

    <Directory "/home/vagrant/application/public">
        Require all granted
        AllowOverride all
    </Directory>
</VirtualHost>

As indicated Apache is listening on Port 8080 and NGINX is listening on port 80 with the following config. 如所示,Apache正在使用以下配置监听端口8080,而NGINX正在监听端口80。

server {
    listen 80;
    server_name application.test www.application.test;

    root /home/vagrant/application/public;
    index index.php index.html;

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

    location ~ \.php$ {
        proxy_pass http://localhost:8080;
        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;
    }

    location ~ /\. {
      deny all;
    }
}

I can confirm that all Laravel requirements are being fulfilled, so i have MBString, OpenSSL, etc... I can also confirm that the rewrite module is working as i can add a redirect clause to my .htaccess and it does get executed as expected. 我可以确认所有Laravel要求都得到满足,因此我拥有MBString,OpenSSL等。我还可以确认重写模块正在工作,因为我可以向.htaccess添加重定向子句,并且确实可以按预期执行。

This is my .htaccess file. 这是我的.htaccess文件。

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

Am i possibly missing something here? 我可能在这里想念东西吗? I've configured NGINX and PHP-FPM before but it's my first time doing it with Apache, i've gone through multiple similar questions but none of the proposed fixes has yet worked for me and that's why i ask this question. 我之前已经配置了NGINX和PHP-FPM,但这是我第一次使用Apache,我经历了多个类似的问题,但是所提出的修复程序都不适用于我,这就是为什么我问这个问题。

Thank you in advance for your help 预先感谢您的帮助

EDIT 编辑

Some progress. 一些进步。

If i set the location config for NGINX as follows 如果我如下设置NGINX的位置配置

location / {
    proxy_pass http://localhost:8080;
    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;
}

Then it works, but to the best of my knowledge then this means that even static file requests would end up proxied to Apache. 然后它起作用了,但是据我所知,这意味着即使是静态文件请求也将最终代理到Apache。

location ~ \\.php$ doesn't seem to work as the laravel pretty url doesn't end with a ".php" suffix i guess. location ~ \\.php$似乎不起作用,因为我猜想laravel漂亮的URL并未以“ .php”后缀结尾。 But how would one go about making these two play nice with one another? 但是,如何使这两个玩得很好呢?

The idea i guess is that you have a default location that directs to index.php and when that index.php file is triggered then nginx receives another HTTP request that matches your php location block and will proxy the request to Apache. 我猜的想法是,您有一个指向index.php的默认位置,并且当触发index.php文件时,nginx会收到另一个与您的php location块匹配的HTTP请求,并将该请求代理到Apache。

The issue is that during this second request, all knowledge of the URI was lost and the URI is now just "index.php" so as far as Apache knows you're always making requests to /index.php . 问题在于,在第二个请求期间,所有关于URI的知识都丢失了,而URI现在只是“ index.php”,据Apache知道您一直在向/index.php发出请求。

I still don't know how to fix it though. 我仍然不知道如何解决它。

So i think i've figured it out, if you have a similar problem, a possible solution is to add the $uri variable to the try_files directive 所以我想我已经解决了,如果您遇到类似的问题,可能的解决方案是将$uri变量添加到try_files指令中

server {
    listen 80;
    server_name application.test www.application.test;

    root /home/vagrant/application/public;
    index index.php index.html;

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

    location ~ \.php {
        proxy_pass http://localhost:8080;
        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;
    }

    location ~ /\. {
      deny all;
    }
}

That way, when nginx calls your index.php it'll call it like http://application.test/index.php/hello and this is the URL that Apache ultimately receives and processes through PHP. 这样,当nginx调用您的index.php时,它将像http://application.test/index.php/hello一样被调用,这是Apache最终通过PHP接收和处理的URL。

So then, .htaccess comes into play, Laravel does it's thing and everything works, and this still only affects PHP files, your static files should still be served directly by NGINX. 因此,.htaccess起作用了,Laravel做到了,一切都可以正常工作,而且这仍然只影响PHP文件,您的静态文件仍应由NGINX直接提供服务。

If you find anything wrong with this approach please let me know. 如果您发现此方法有任何问题,请告诉我。

UPDATE UPDATE


A dumb mistake on my end later down the line lead me to believe something else was wrong, every route seemed to display the contents of my "/" root route. 后来我的行上出现了一个愚蠢的错误,使我相信其他错误,每条路由似乎都显示了我的“ /”根路由的内容。 After trying to use Homestead instead of my custom setup i was surprised to find it affected Homestead as well, so it had to be related to my app itself and not the Server. 在尝试使用Homestead而不是我的自定义设置后,我很惊讶地发现它也影响了Homestead,因此它必须与我的应用程序本身有关,而与服务器无关。

Basically it was the Exception handler returning the root view because my requests were missing some data required by my FormRequests, after changing my Exception handler class to handle JSON responses it worked as expected. 基本上,是异常处理程序返回了根视图,因为更改了异常处理程序类以处理按预期方式工作的JSON响应后,我的请求丢失了FormRequests所需的某些数据。

If you fall into this kind of trap, try to make sure your app works with Homestead, if it does then there's something wrong with your vagrant setup, if not, then it's your Laravel app that's to blame. 如果您陷入这种陷阱,请尝试确保您的应用程序可与Homestead一起使用,如果可以,则说明您的无业游民设置存在问题,否则,应该归咎于Laravel应用程序。

If your Exception Handler just does the typical render, then be wary of the fact it'll attempt to redirect to the previous route, which if you're developing an API with Postman will be "/". 如果您的异常处理程序只是执行典型的渲染,请警惕它会尝试重定向到先前的路由,如果您正在使用Postman开发API,则它将为“ /”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM