Upgreidas's avatar

Laravel behind Nginx reverse proxy always throws 404

I have clean Laravel 10 app running in docker container using sails and nginx web server (in main server, not a docker container) with reverse proxy to Laravel app. No matter which path I try I always get 404 error. And this error is thrown from Laravel, not Nginx. I see Laravel's 404 template. That means my request reaches Laravel app but it throws 404 error somewhere in runtime. I tried disabling middlewares, but with no luck. If I open Laravel app directly, not through proxy, everything is fine. I know Laravel Sail should not be used in production, but I need this, don't ask why.

docker-compose.yml

version: "3"
services:
    laravel.test:
        build:
            context: ./vendor/laravel/sail/runtimes/8.2
            dockerfile: Dockerfile
            args:
                WWWGROUP: "${WWWGROUP}"
        image: sail-8.2/app
        extra_hosts:
            - "host.docker.internal:host-gateway"
        ports:
            - "${APP_PORT:-80}:80"
            - "${VITE_PORT:-5173}:${VITE_PORT:-5173}"
        environment:
            WWWUSER: "${WWWUSER}"
            LARAVEL_SAIL: 1
            XDEBUG_MODE: "${SAIL_XDEBUG_MODE:-off}"
            XDEBUG_CONFIG: "${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}"
            IGNITION_LOCAL_SITES_PATH: "${PWD}"
        volumes:
            - ".:/var/www/html"
        networks:
            - sail
networks:
    sail:
        driver: bridge

Nginx config

server {
    listen 80;
    server_name website.local;
    client_max_body_size 32M;

    location /admin/ {
        access_log /var/log/nginx/admin_access.log;
        error_log /var/log/nginx/admin_error.log;

        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://laravel.test:8080;
        proxy_redirect off;
    }

}

Any ideas what might be at fault?

0 likes
3 replies
Upgreidas's avatar

@Snapey default

<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]

    # Send Requests To Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>
Upgreidas's avatar

@Snapey I tried adding RewriteBase /admin/ after RewriteEngine On as well but it did not help.

Please or to participate in this conversation.