简体   繁体   中英

Nginx with FPM and Laravel

I have a docker image with several containers (nginx, api, react, ...) and two server blocks inside nginx.conf which gives me option to access via domain.com and api.domain.com .

Everything works so far, if I access via domain.com, it will point me to react container and port 3000. If I access via api.domain.com, it will point me to laravel app (php-fpm port 9000).

Now, I would like to "break" config to point me to laravel if I type domain.com/api or domain.com/api/anything/here , but to have react still working.

I almost got it, it will point me to laravel container (api container), but the problem is that it doesn't parse PHP correctly (remember that it works fine via api.doamin.com). I can see some laravel output, but in plain text.

This is what I have inside block for react (domain.com):

location /api {
    alias /var/www/api/public/;   
    try_files $uri $uri/ /index.php?$query_string;
}
    
location ~ \.php$ {
    root /var/www/api/public;
    
    resolver 127.0.0.11;
    set $api api;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass $api:9000;
    fastcgi_index index.php;
    include fastcgi_params; 
    fastcgi_param SERVER_NAME $host;

    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

# This part still works fine
location / {
    index index.html;
    root /var/www/app;
    resolver 127.0.0.11;
    set $reactjs reactjs;
    proxy_pass http://$reactjs: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;
}

Best regards.

You should add fpm version path to your config file if You have installed php-fpm extension (depend on your php version). Example:

php-fpm extension for php 7.2

 sudo apt-get install php7.2-fpm 

php-fpm extension for php 8.0

 sudo apt-get install php8.0-fpm 

a part of your ngnix config:

location ~ \.php$ {
    root /var/www/api/public;
    
    resolver 127.0.0.11;
    set $api api;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;

    fastcgi_pass $api:9000;
    fastcgi_index index.php;
    include fastcgi_params; 
    fastcgi_param SERVER_NAME $host;

    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    #new config lines
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php8.0-fpm.sock; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM