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

Zalarudo's avatar

Laravel 8 all routes are nginx 404 error except /.

I'm having trouble deploying a laravel 8 project on a product server. After installation, migration, etc, all my routes return 404 nginx error except "/". And also all styles and scripts connect to the index page only if you add in the URL /index.php

Here is my folder structure

htdocs
--public
	--app
	--bottstrap
	--public
	--.htaccess				
	--etc.

index.php is on htdocs/public/public directory, so for set all request to this folder, i'll setup my htaccess on simple way

RewriteEngine On
RewriteRule ^(.*)$ public/ [L]

I guess, this problem may be caused from nginx.conf or apache.conf, but it realy hard to understand where is a trouble

my nginx.conf


server {
    listen 80;
    server_name domain.com www.domain.com;

    include letsencrypt.conf;

    location / {
        root  /srv/www/domain.com/htdocs/public;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php$is_args$args;

    }

    access_log off;
    error_log /srv/www/domain.com/logs/domain.com-err;
}

server {
    listen 443 ssl;
    server_name domain.com www.domain.com;
    #charset    utf-8;
    server_name_in_redirect off;

    if ($host ~ www.domain.com) { rewrite ^ domain.com$request_uri? permanent; }

    ssl_certificate /etc/letsencrypt/live/domain.comfullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;

    access_log off;
    #access_log /srv/www/domain.com/logs/domain.com-acc main;
    error_log /srv/www/domain.com/logs/domain.com-err;

    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_set_header    Host $host;

    set $proxyserver    "http://127.0.0.1:8080";
    set $imcontenttype  "text/html; charset=utf-8";
    set $docroot        "/srv/www/domain.com/htdocs/public";

    index index.php;
    root /srv/www/domain.com/htdocs/public;

    # Include parameters common to all websites
    include bx/bitrix.conf;
}

and here is my apache.conf

<VirtualHost *>
    DocumentRoot /srv/www/domain.com/htdocs/public
    ServerName domain.com
    ServerAlias www.domain.com
    ErrorLog /srv/www/domain.com/logs/domain.com-err
    # CustomLog /srv/www/domain.com/logs/domain.com-acc combined
    <IfModule mod_setenvif.c>
        SetEnvIf X-Forwarded-Proto https HTTPS=on
    </IfModule>

        <IfModule mpm_itk_module>
                AssignUserID domain.com domain.com
        </IfModule>

        <IfModule mpm_prefork_module>
                RMode config
                RUidGid domain.com domain.com
                RGroups apache
        </IfModule>

    <Location />
        php_admin_value open_basedir "none"
        php_admin_value upload_tmp_dir "/srv/www/domain.com/tmp"
        php_admin_value session.save_path "/srv/www/domain.com/tmp"
        php_admin_value upload_max_filesize "256M"
        php_admin_value post_max_size "256M"
        php_admin_value memory_limit "512M"
        php_admin_value date.timezone "UTC"
        php_admin_value max_input_vars "10000"
        php_admin_value pcre.recursion_limit "100000"
        php_admin_value realpath_cache_size "4096k"
        php_admin_value mbstring.internal_encoding "utf-8"
        php_admin_value mbstring.func_overload "0"
	php_admin_value opcache.revalidate_freq "0"
        php_admin_value opcache.max_accelerated_files "100000"
        php_admin_value opcache.memory_consumption "256"
        #php_admin_flag short_open_tag "On"
    </Location>

    <DirectoryMatch .*\.svn/.*>
         Require all denied
    </DirectoryMatch>

    <DirectoryMatch .*\.git/.*>
         Require all denied
    </DirectoryMatch>

    <DirectoryMatch .*\.hg/.*>
         Require all denied
    </DirectoryMatch>

    <Directory /srv/www/domain.com/htdocs/public>
        Options Includes FollowSymLinks
        AllowOverride All
        Require all granted
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/cache>
        AllowOverride none
        Require all denied
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/managed_cache>
        AllowOverride none
        Require all denied
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/local_cache>
        AllowOverride none
        Require all denied
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/stack_cache>
        AllowOverride none
        Require all denied
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/upload>
        AllowOverride none
        AddType text/plain php,php3,php4,php5,php6,phtml,pl,asp,aspx,cgi,dll,exe,ico,shtm,shtml,fcg,fcgi,fpl,asmx,pht
        php_value engine off
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/upload/support/not_image>
        AllowOverride none
        Require all denied
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/images>
        AllowOverride none
        AddType text/plain php,php3,php4,php5,php6,phtml,pl,asp,aspx,cgi,dll,exe,ico,shtm,shtml,fcg,fcgi,fpl,asmx,pht
        php_value engine off
    </Directory>

    <Directory /srv/www/domain.com/htdocs/public/bitrix/tmp>
        AllowOverride none
        AddType text/plain php,php3,php4,php5,php6,phtml,pl,asp,aspx,cgi,dll,exe,ico,shtm,shtml,fcg,fcgi,fpl,asmx,pht
        php_value engine off
    </Directory>

    <IfModule mod_rewrite.c>
        #Nginx should have "proxy_set_header HTTPS YES;" in location
        RewriteEngine On
        RewriteCond %{HTTP:HTTPS} =YES
        RewriteRule .* - [E=HTTPS:on,L]
    </IfModule>

</VirtualHost>



0 likes
16 replies
Zalarudo's avatar

@Tray2 Thank u for your advice. I have a few not clever questions.

  1. i configured my nginx conf by your advice (and reload it of course) but it doesnt help
  2. i doesnt know nginx well, so maybe a do something wrong
  3. Can it be problem because of ssl? In my conf i have 2 servers one on 80 port and another on 443
Zalarudo's avatar

@Tray2 Could u give me a poit about my idea? Can i just disable nginx via conf file (i have permission only on it) and setup my project via apache conf? I think it would be easy.

Zalarudo's avatar

@Tray2 sadly, but i dont have access to configure production server. I have permission only for change nginx.conf and apache.conf. This is why i asked about disable nginx visa .conf file

Tray2's avatar

@Zalarudo You can't run both of them at the same port.

If you configure nginx to use 443, apache can't be using the same port.

It is always a bad idea to have two different web-servers installed on the same machine, they will interfere with each other unless properly configured.

kokoshneta's avatar

YOUR APP IS COMPLETELY UNSAFE – DO NOT USE!

You should NEVER put all your Laravel files inside the htdocs/public folder! That’s the public folder, meaning that anyone can access all your files just by typing in the URL – i.e., going to https://example.com/.env will just load your .env file with all your passwords and everything.

You need to move your files up a level, so you have this structure:

htdocs
-- app
-- bootstrap
-- public
-- resources
-- routes
-- etc.

You don’t need the .htaccess file. And as @tray2 says, your Apache config file doesn’t do anything if you’re using Nginx. Just ignore it.

Zalarudo's avatar

@kokoshneta For now i just trying to run it, after this, i ll going to change structure. For now in nginx conf i changed root folder directly for htdocs/public/public assets now including well, but all routes still have 404 nginx error

Zalarudo's avatar

@kokoshneta if u could look on my nginx conf and give me a hint where is mistake, i ll be very greatfull) Cause im a noobie for this staff

Zalarudo's avatar

@kokoshneta yes, now i have 502 error on routes and

2023/03/18 00:32:58 [crit] 24136#24136: *2416 connect() to unix:/var/run/php/php8.1-fpm.sock failed (2: No such file or directory) while connecting to upstream, client: MY_IP, server: domain.com, request: "GET /content HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "domain.com", referrer: "https://domain.com/"
Zalarudo's avatar

@kokoshneta Also i added this sets in nginx.conf

  error_page 404 /index.php;
    location ~ \.php$ {
         fastcgi_pass unix:/var/run/php/php8.1.6-fpm.sock;
         fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
         include fastcgi_params;
     }

Zalarudo's avatar

@kokoshneta and i tryed to check php-fpm status and this what i've got

$ service php-fpm status
Redirecting to /bin/systemctl status php-fpm.service
● php-fpm.service - The PHP FastCGI Process Manager
   Loaded: loaded (/usr/lib/systemd/system/php-fpm.service; enabled; vendor preset: disabled)
   Active: active (running) since Fri 2023-03-10 15:24:27 MSK; 1 weeks 0 days ago
 Main PID: 3186 (php-fpm)
   Status: "Processes active: 0, idle: 5, Requests: 0, slow: 0, Traffic: 0req/sec"
   CGroup: /system.slice/php-fpm.service
           ├─3186 php-fpm: master process (/etc/php-fpm.conf)
           ├─3187 php-fpm: pool www
           ├─3188 php-fpm: pool www
           ├─3189 php-fpm: pool www
           ├─3190 php-fpm: pool www
           └─3191 php-fpm: pool www



kokoshneta's avatar

@Zalarudo I don’t know what might cause that, to be honest. Looks like some sort of misconfiguration of either PHP or Nginx, but I don’t really know much about Nginx at a deeper level, and I’m not a Unix user, so I’m not much help on that…

Zalarudo's avatar

@kokoshneta so am i :( Anyway thanks a lot for your help. After 3 days pain, i have a new error xD

Please or to participate in this conversation.