we are running nginx + php5-fpm and as part of our deployment process we use a symlink to point to the latest version of our PHP code.
In order to avoid problems with changing files when deploying new versions, we use the following nginx-config, which uses $realpath_root to send the real file path to php-fpm:
location ~* ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
This setup worked well for the last year while we used AWS instances. Now we switched to Google Cloud and every time we deploy a new version, our servers response time increases by factor 2-3.
After reloading PHP-fpm using kill -USR2 17 (where 17 is the process ID of the master process), response times recover to normal values.
Any suggestions how to solve this without restarting php-fpm worker processes after every release?
Best,
Tobias
Related
I'm trying to setup an nginx reverse proxy, deployed as a networked image in ECS. Behind it is a PHP API, running in a separate php-fpm container listening on port 9000. The nginx docs and examples I can find seem to suggest that it has to be configured with a root path within the server block in the config.
The following is an example (from here) of the nginx config that seems to be commonly used in this scenario:
server {
listen 80;
root /var/www/html/public; # is this really needed if proxying all requests to php?
server_name _;
index index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php-fpm:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
Looking at it from a performance perspective, when a request comes into an API endpoint such as /api/foobar, in this particular scenario there is no point in nginx trying to check the filesystem for an index file inside this folder, since we know for sure there is no such folder. Ideally it should bypass this unnecessary step and simply proxy the request straight to the other container that's running php-fpm, letting it deal with route matching and 404's if an unexpected route is requested.
So, my question is - given a requirement to only serve API requests via a PHP application with "virtual routes" served up via a single index.php entry point (such as Slim) with no need to serve up any static files from a "root" path - is there a way to configure nginx to just proxy all incoming requests using fastcgi_pass?
I found a similar question here but it didn't quite answer my question.
I've always installed my stack locally for development, NginX, PHP7, MySQL, and Couchbase. No Problems.
Now I have to work on a project that requires a lower version of PHP... And my team will have to work on it as well, so I've looked to Docker to try to find the solution.
In my existing NGINX conf files I send the requests off to php-fpm like this
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_ignore_client_abort on;
fastcgi_param SERVER_NAME $http_host;
}
See the line
fastcgi_pass 127.0.0.1:9000;
?
I was hoping to be able to create a docker container running a specific version of PHP and write it into the server block as above but with
fastcgi_pass 172.17.0.1:9000;
Where 172.17.0.1 is the IP of the container.
I have used, very simply,
FROM php:7.1-fpm
EXPOSE 9000
As my dockerfile. I can build an image, run the container, run bash in the container and see that PHP -I and PHP -v return what I expect.
Running docker inspect has given me two IP addresses, 172.17.0.1, and 172.17.0.2 (I've tried both in the example above)
However, this set up is not working - when I try to visit the site in a browser I get an NGINX 504 gateway timeout error.
Guessing I'm missing something, but not sure what.
Happy to use docker compose if I need to, happy to mount volumes into the container if I need to. Just not sure what I need!:)
I'm honestly just getting quite frustrated. I feel like I keep fiddling with my nginx conf, and I'll get it working, until I restart my PC or docker. All of the docker functionality seems to be working fine, I can build and docker-compose up without problem, but it seems like every time I restart my computer or docker I have to fiddle with my nginx conf again until it works. I'm using wordpress, and currently have wordpress installed to a subdirectory wp and my wp-content in a different subdirectory at content.
My nginx configuration looks like this:
server {
listen 80;
root /var/www;
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri $uri/index.php /index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass app:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
and it was working yesterday while I was working, but after restarting the PC overnight, and trying to work on the site today, I'm getting the standard php-fpm issue FastCGI sent in stderr: "Primary script unknown". I'm just curious if anybody has a solution, or knows why I'm having this issue. I understand that the "Primary script unknown" error usually means that my SCRIPT_FILENAME param is bad, but it was working yesterday, and as far as I can tell should always point to /var/www/index.php, which is where my wordpress index file is.
Should I basically just hardcode $document_root/index.php ?
EDIT: I literally just rebuilt my docker containers with docker-compose build and everything is working as intended. So I guess this question is more associated with docker than nginx/php, but I would still love some guidance. Would there be some reason my docker containers are ephemeral and need rebuilding on boot everytime?
Trying to setup Magento2 on my LEMP stack. Was following the instructions here and here (did that after compiling from sources for multiple time since it was hard to fulfill composer requirements for Magento2)
Installed composer
Configured and run php, php-fpm
Did some trivial tests (success)
However, a blank screen is all I get. The nginx configuration
server {
listen 2000;
root /usr/share/nginx/html/magento2;
index index.php;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
include fastcgi_params;
}
}
The permission set is to -R 777 (test purpose only), user and group is www-data. Was able to run .php scripts, problem applies to Magento2.
Read also some related issues (without any positive results) :
Nginx configuration with Magento 1.8
ngix and php5-fpm blank page
The above query is incomplete the nginx configuration.
Please use the below link configuration of nginx.
http://gotechnies.com/magento2-lemp-nginx-ubuntu/
i'm newbie in openshift and nginx. This is my first experiment creating DIY app using php 5.5 on nginx. Nginx server is working perfectly, but when i add php-fpm configuration :
...
location ~ ^/index.php(/|$) {
root html;
include fastcgi_params;
fastcgi_pass unix:${OPENSHIFT_RUN_DIR}/php-fpm.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME html/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_intercept_errors on;
}
...
in nginx.conf, I get 503 error on my browser telling service is unavailable. I had modify above script by many suggestion, but still no luck. Please anyone? Thanks
ps : I also had following https://www.openshift.com/forums/openshift/installing-php-fpm-and-nginx from github, but still no luck
I suggest you creating a Downloadable cartridge instead of DIY. There are several nginx-php community cartridges on GitHub at the moment, eg. OpenShift Nginx PHP-FPM Cartridge by Getup Cloud, which you can spin up very easily with a single command:
rhc app create php https://reflector-getupcloud.getup.io/reflect?github=getupcloud/openshift-nginx-php-fpm
References:
OpenShift Origin Documentation Site
OpenShift Origin Cartridge Developer’s Guide
Download and Run Your Own Cartridges on OpenShift
List of OpenShift Origin community cartridges (an early instance of oo-index, see it's GitHub repo)