1

I have a Laravel app that works perfectly fine when running with php artisan serve but when trying to use nginx as the frontend I'm having all sorts of trouble getting this to work properly.

I basically have this structure:

root@server:/var/www/html# tree -d -L 4
.
`-- app
    |-- C4
    |   `-- www
    |       |-- sites
    |       `-- site-assets
    `-- prod
        |-- app
        |-- bootstrap
        |-- config
        |-- database
        |-- public
        |-- resources
        |-- routes
        |-- storage
        `-- vendor

Where prod contains the laravel app

I can get the main app to open but there is a script in prod/public/js which checks to see if a directory in C4/www/sites/ exists which isn't working and I suspect that my nginx config is the culprit.

Below is my nginx config:

server {
    listen 0.0.0.0:80;
    listen [::]:80;
    root /var/www/html/app/prod/public;
    index index.php;

    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Disclaimer: I'm not the dev of this app. I'm merely a systems engineer helping out. The devs don't have any experience with nginx

EDIT: adding some errors I'm seeing in /var/log/nginx/error.log :

2023/02/07 22:25:17 [error] 91535#91535: *23 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.x.x, server: , request: "GET /checksite/domain.com HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php8.1-fpm.sock:", host: "192.168.x.x"

UPDATE: changed the root to /var/www/html/app/prod/public and the index to just index.php. The site loads but there's some script that runs which checks for a directory outside of the root to see if it exists and if it doesn't it creates it. What's happening now is that the directory is being created inside the public directory within the laravel app.

UPDATE 2: I'm seeing a jQuery error in the browser console

Uncaught SyntaxError: Unexpected non-whitespace character after JSON at position 2

I will take this up with the devs in the morning to see if they can help debug this.

  • "all sorts of trouble"/"isn't working" could be many things.. please quote some specific log/error – anx Feb 07 '23 at 22:25
  • just added an error that I see is repeated frequently in nginx – MasterOfNothing Feb 07 '23 at 22:29
  • is `client: 192.168.x.x` and `host: "192.168.x.x` the same IP? is that the actual output? did you totally remove some info at `server: ,`? – Jaromanda X Feb 07 '23 at 22:30
  • @JaromandaX no it's not but they are on the same subnet – MasterOfNothing Feb 07 '23 at 22:31
  • fair enough, not sure why you obfuscated private IP's, or is that the actual output? – Jaromanda X Feb 07 '23 at 22:32
  • @JaromandaX just obfuscating. Force of habit I guess lol – MasterOfNothing Feb 07 '23 at 22:34
  • [this](https://stackoverflow.com/questions/35261922/how-to-debug-fastcgi-sent-in-stderr-primary-script-unknown-while-reading-respo) and [this](https://serverfault.com/questions/517190/nginx-1-fastcgi-sent-in-stderr-primary-script-unknown) were the frist two results when researching that error - not sure they help, but the answers have a lot of upvotes :p – Jaromanda X Feb 07 '23 at 22:36
  • FYI `try_files $uri $uri/ /index.php?$args;` will try to execute the file `/index.php` if the URL doesn't point to a valid file, which could be messing with the `SCRIPT_FILENAME` value, hence the `Primary script unknown` error. – drmad Feb 07 '23 at 22:51
  • @drmad I already had that one in my config – MasterOfNothing Feb 07 '23 at 23:35
  • @JaromandaX I made some changes (updated the original post with the details) and now that error has gone away. What I'm getting now are some jquery related errors. Will update that now. – MasterOfNothing Feb 07 '23 at 23:36

0 Answers0