strangeQuirks
strangeQuirks

Reputation: 5940

nginx configuration for a nodejs app and laravel app together

So I have a web app project on my server with 2 directories: 1) frontend - a nodejs app (next.js) that I start with pm2. 2) api - the laravel backend api

I also the have nginx installed and tried this as my server settings:

server {
    location / {
        proxy_pass http://localhost:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

    listen 80;
    listen [::]:80;
    root /project/api/public;
    index index.php index.html index.htm;

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

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

Now when i go to my web address the frontend part works so when i go to example.com I see my web app (so the config for the proxy to port 3000 works).

Howvever the backend/api does not work, i think because it does not redirect to the api when i perform a backend task. like http://example.com:3000/api/auth/login/ should go to my laravel app.

"/api/..." is the backend endpoints.

I think I need to somehow define a location /api {} to go to the laravel app.

Any help to get ths working would be appreciated.

Upvotes: 0

Views: 1548

Answers (1)

Michael Miller
Michael Miller

Reputation: 399

You have 2 "location /" definitions... One is clobbering the other.

Try using the following for your second definition:

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

Upvotes: 0

Related Questions