Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

FireBlade's avatar

Laravel app on production returns error 404 on everything except index page

I have successfully installed Jetstream. npm run dev runs fine. However, I can only access the index page- all other pages including login and register return 404. Here's my setup :

server {

    server_name example.com www.example.com;

    listen [::]:443 ssl http2 ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot
    #include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_ciphers EECDH+CHACHA20:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
    add_header Strict-Transport-Security "max-age=15768000" always;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
    server_tokens off;
    ssl_buffer_size 8k;
    ssl_protocols TLSv1.2 TLSv1.1 TLSv1;
    ssl_ecdh_curve secp384r1;
    ssl_session_tickets off;
    charset utf-8;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    ssl_stapling on;
    ssl_stapling_verify on;
    resolver 8.8.8.8;
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9001;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    root /var/www/html/public;
    index index.php index.html;
    charset utf-8;
    location ~ /\.(?!well-known).* {
        deny all;
    }
}

My Laravel .env file has this configuration:

APP_URL=http://localhost

My docker-compose.yml file looks like this:


version: "3.7"
services:
  app:
    build:
      args:
        user: sammy
        uid: 1000
      context: ./
      dockerfile: Dockerfile
    image: laraapp
    container_name: laraapp-app
    restart: unless-stopped
    working_dir: /var/www/html/
    volumes:
      - web-root:/var/www/html
    ports:
      - "9001:9000"
    networks:
      - laranet
  db:
    image: mysql/mysql-server:8.0
    container_name: laraapp-db
    restart: unless-stopped
    tty: true
    environment:
      MYSQL_DATABASE: ${DB_DATABASE}
      MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD}
      MYSQL_PASSWORD: ${DB_PASSWORD}
      MYSQL_USER: ${DB_USERNAME}
      SERVICE_TAGS: dev
      SERVICE_NAME: mysql
    volumes:
      - ./mysql:/etc/mysql/conf.d/
      - mysqldata:/var/lib/mysql
    networks:
      - laranet

  nginx:
    image: nginx:1.21-alpine
    container_name: laraapp-nginx
    tty: true
    restart: unless-stopped
    ports:
      - "8444:443"
    volumes:
      - web-root:/var/www/html
      - ./nginx:/etc/nginx/conf.d/
    depends_on:
      - app
    networks:
      - laranet
networks:
  laranet:
    driver: bridge

# Volumes
volumes:

  mysqldata:
  web-root:
    driver: local
    driver_opts:
      type: none
      device: /home/sammy/laraapp/src/
      o: bind


I have a NGINX reverse proxy on my host machine forwarding requests to post 8444 like so:

location / {
        try_files $uri $uri/ =404;
        proxy_pass https://localhost:8444;
        proxy_redirect     off;
        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 $server_name;
    }

php artisan route:list returns all the correct app routes.

0 likes
7 replies
Nihir's avatar

There are many possibilities for getting an error page 1: Check your .env file's APP_URL=http://127.0.0.1 which can not be updated with your domain name like this APP_URL=http://yourdomain.com.

2: The second thing is a public directory in your root folder. There is an index.php file and some other folders and files. Cut that all and paste it into the root folder and change the directory path that index file your index file like this:

index.php

if (file_exists($maintenance = __DIR__.'/storage/framework/maintenance.php')) {
    require $maintenance;
}

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';

And don't forget to delete that public folder after your cut-paste is done.

And once you change this configuration, you have just config your cache in the root directory terminal.

php artisan optimize:clear
PHP artisan config:cache

Done, you have just hard to refresh your page.

FireBlade's avatar

@Nihir Your recommendation looks a little off...why move files from the public folder ? This is my second app on production and the first one works without such changes.

Tray2's avatar

Make sure that you have the correct document root

- web-root:/var/www/html

It should probably be

- web-root:/var/www/html/public
Tray2's avatar

@FireBlade I don't know how you set the document root for nginx in docker, gut I know that it should point to the public directory and not the html directory.

I know @sinnbeck uses docker, so he probably knows.

1 like
FireBlade's avatar
FireBlade
OP
Best Answer
Level 2

SOLVED. This is the offending line;

try_files $uri $uri/ =404;
1 like

Please or to participate in this conversation.