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

dexvanoni's avatar

Nginx does not work with laravel

My sites-avaliable/default configuration

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

root /var/www/html/;
index index.php index.html index.htm index.nginx-debian.html;

server_name 10.152.16.97;

sendfile off;

    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

location / {
    try_files $uri $uri/ /index.php?$query_string;

}

location /tapio2018 {
    root /var/www/html/tapio2018/public;
    #rewrite ^/tapio2018/(.*)$ / break;
    rewrite ^/tapio2018/(.*)$ /index.php?__path__=/ last;
    try_files $uri $uri/ /index.php?$args;

}
location ~ \.php$ {

            set $newurl $request_uri;

            if ($newurl ~ ^/tapio2018(.*)$) {
                   set $newurl ;
                  root /var/www/html/tapio2018/public;
            }

            # Pass all PHP files to fastcgi php fpm unix socket
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #fastcgi_pass unix:/var/run/php5-fpm.sock;      #debian php5
            fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; #debian php7
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param REQUEST_URI $newurl;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_intercept_errors off;
            fastcgi_buffer_size 16k;
            fastcgi_buffers 4 16k;
        }
location ~ /\.ht {
    deny all;
}
}

The first page appears without css and js. The other routes appear: No input file specified.

0 likes
22 replies
cmdobueno's avatar

I only use nginx and have never had a problem with laravel.

Just looking over your conf file, there are issues. See @elvijs 's response and look at the example nginx files located at the provided link.

1 like
dexvanoni's avatar

@elvijs @cmdobueno on this same server is running the GLPI normally. I'm guessing the problem is around here:

set $newurl $request_uri;

            if ($newurl ~ ^/tapio2018(.*)$) {
                   set $newurl ;
                  root /var/www/html/tapio2018/public;
            }

I looked at your setup @elvijs however, my server will have multiple applications in nginx and only this is laravel.

ejdelmonico's avatar

Take the advice above. nginx works very well with Laravel. Your config file is a mess. Look over the basic example link above and this exerpt:

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com;
    root /home/example.com;
    
    # HSTS
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    ssl_certificate /etc/nginx/ssl/example.com/255573/server.crt;
    ssl_certificate_key /etc/nginx/ssl/example.com/255573/server.key;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA';
    ssl_prefer_server_ciphers on;
    ssl_dhparam /etc/nginx/dhparams.pem;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    location ~* \.(?:manifest|appcache|html?|xml|json)$ { expires -1; }
    location ~* \.(?:css|js)$ { expires 1y; access_log off; add_header Cache-Control "public"; }
    location ~* \.(?:ttf|ttc|otf|eot|woff|woff2)$ { expires 1M; access_log off; add_header Cache-Control "public"; }
    location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm|htc)$ { expires 1M; access_log off; add_header Cache-Control "public"; }

    access_log off;
    error_log  /var/log/nginx/example.com-error.log error;

    error_page 404 /index.php;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }

and, NO, the cert numbers are not real for ssl. Also, if you run nginx -T on newer versions, it will give you the full nginx configuration. Additionally, nginx -t checks your config file.

1 like
dexvanoni's avatar

@ejdelmonico return command nginx -t:

root@debian:/etc/nginx/sites-available# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

other applications usually work with these settings. Only the setting for 10.152.16.97/tapio2018 does not work.

dexvanoni's avatar

@elvijs return:

root@debian:/etc/nginx/sites-available# php --ini |grep Loaded
Loaded Configuration File:         /etc/php/7.0/cli/php.ini

elvijs's avatar

@dexvanoni , type vi /etc/php/7.0/cli/php.ini or nano /etc/php/7.0/cli/php.ini and check if cgi.fix_pathinfo is uncommented and set to 0:

cgi.fix_pathinfo=0

instead of:

;cgi.fix_pathinfo=1
elvijs's avatar

@dexvanoni , after that change execute sudo systemctl restart php7.0-fpm.service and let me know if that fixes it.

dexvanoni's avatar

@ejdelmonico I made the configuration according to your post. The return was: 403 Forbidden

my error.log:

2018/02/21 15:25:04 [error] 19637#19637: *6 directory index of "/var/www/html/tapio2018/" is forbidden, client: 10.152.16.76, server: 10.152.16.97, request: "GET /tapio2018/ HTTP/1.1", host: "10.152.16.97"

my default file:

root@debian:/etc/nginx/sites-available# cat default 
server {
        listen 80 default_server;
        listen [::]:80 default_server ipv6only=on;

        root /var/www/html/;
    index index.php index.html index.htm index.nginx-debian.html;
        server_name 10.152.16.97;

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

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

my tapio2018 file:

root@debian:/etc/nginx/sites-available# cat tapio2018 
server {
        listen 80;

        root /var/www/html/tapio2018/public;
        index index.php index.html index.htm;

        server_name 10.152.16.97/tapio2018;

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

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}

reminding me that I created the link to:

/etc/nginx/sites-enabled

elvijs's avatar

So, if I understand correctly we're dealing w/ domain & sub-domain scenario, e.g., you want nginx to utilize /var/www/html resources when example.com is visited, but when tapio2018.example.com is visited, you want nginx to use /var/www/html/tapio2018/public resources?

dexvanoni's avatar

@elvijs exactly. is that other applications like GLPI have not been developed with Laravel and are working correctly.

elvijs's avatar

Check if these return something similar to:

ls -l /etc/nginx/sites-available/

... default
... tapio2018
ls -l /etc/nginx/sites-enabled/

... default -> /etc/nginx/sites-available/default
... tapio2018 -> /etc/nginx/sites-available/tapio2018

Also a reminder to run these cmds. after making symb. links:

sudo nginx -t

sudo systemctl restart nginx.service
sudo systemctl enable nginx.service
sudo systemctl enable php7.0-fpm.service
dexvanoni's avatar

@elvijs I already performed all the commands:

root@debian:/etc/nginx/sites-available# ls -l /etc/nginx/sites-enabled/
total 0
lrwxrwxrwx 1 root root 34 set 18 14:26 default -> /etc/nginx/sites-available/default
lrwxrwxrwx 1 root root 36 fev 21 15:29 tapio2018 -> /etc/nginx/sites-available/tapio2018

message 403 still appears!

elvijs's avatar

Ok, what if you change server_name 10.152.16.97/tapio2018; to server_name tapio2018.10.152.16.97; in your /etc/nginx/sites-available/tapio2018 file? Remember, when you make these changes, you must delete a symbolic link, reapply it, and restart it all.

If this doesn't work, then I am out of ideas...

dexvanoni's avatar

@elvijs DNS error! My server is in my network. his IP is 10.152.16.97 When I type in the browser tapio2018.10.152.16.97 it returns DNS error.

elvijs's avatar

@dexvanoni , ok, try these:

root@debian:/etc/nginx/sites-available# cat default

server {
        listen 80;

        server_name 10.152.16.97;

        root /var/www/html/;
        index index.php index.html index.htm index.nginx-debian.html;

        location ~ \.php$ {
                try_files $uri /index.php =404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }
}
root@debian:/etc/nginx/sites-available# cat tapio2018

server {
        listen 80;

        server_name 10.152.16.97/tapio2018;

        root /var/www/html/tapio2018/public;
        index index.php;

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

        location ~ \.php$ {
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~ /\.ht {
                deny all;
        }
}

Don't forget to remove symb. links, re-apply them and this drill:

sudo nginx -t

sudo systemctl restart nginx.service
sudo systemctl enable nginx.service
sudo systemctl enable php7.0-fpm.service
tcetin's avatar

Hi there,

I don't know solved this issue or not but it is about nginx root definition.

It should be like this

root /home/vagrant/project_root/public

Please don't forget to write public.

Also it sets this settings from Homestead.yaml file. It should be like this in Homestead yaml file:

sites:
    - map: your_project.local
      to: /home/vagrant/code/your_project/public

Please or to participate in this conversation.