Nginx + PHP on docker serving static files - php

I have two containers: nginx and php-fpm. The files are stored on the php-fpm container. There is no problem with serving PHP files, but I need to serve static files as well, and I have a problem with the location configuration.
No idea how to configure it correctly.
location /static/ {
fastcgi_pass api:9000;
}
or I tried:
location /vendor/ {
root api:9000/public/vendor/;
}
This is the config file:
server {
listen 80 default_server;
root /app/api/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location /static/ {
fastcgi_pass api:9000;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_pass api:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_read_timeout 3600;
}
}
Thank you!

As #slauth mentioned, there is no way to access files to another container.
I decided to serve static images using PHP. Here is a good tutorial: https://grahamlikesto.codes/slim-php-how-to-serve-static-files-with-slim-php/
This is not the best solution. My app is a small app with little traffic, and serving assets is not a big deal, but you should change your architecture if you run this issue.

Related

Nginx php rewrite for symfony isn't allowing to access standard php files

Sorry if this is a dumb question, i am quite new to using nginx after switching from apache. I have a symfony app running on nginx and it's working using containers and cgi pass. I am able to access symfony routes on it without an issue, but i also have a single php file in the public folder that needs to be accessed, but it's giving me 404.
I think it's related to the nginx rules but i'm not sure how to get around it
events {
worker_connections 1024;
}
http {
server {
listen 80;
root /usr/share/nginx/html/public;
server_name api.gofollow.vip;
location / {
try_files $uri /index.php$is_args$args;
#try_files $uri $uri/ =404;
}
location ~ \.php$ {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
internal;
}
# deny access to apache .htaccess
location ~ /\.ht {
deny all;
}
error_log /usr/share/nginx/logs/error.log;
access_log /usr/share/nginx/logs/access.log;
}
}
So i need the symfony routes like example.com/page/hello to work, which they do, but i also need to be able to access example.com/tester.php
It can block any other php file, but "tester.php" needs to be accessible.

Nginx + PHP-FPM redirect to static PHP file

Some details about my setup first:
I am serving a static webapp (HTML + JS) from default Nginx webroot
I have a PHP-FPM server running on localhost:9000
The destination file should be /api/webroot/index.php for FPM (always, no need to try_files etc.)
I need to forward all /api and /api-debug calls to arrive at localhost:9000, and the /app/webroot/index.php should handle all these requests.
I have the following working Nginx configuration:
upstream fastcgi_backend {
server localhost:9000;
keepalive 30;
}
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
location ~ ^/(api|api-debug)/ {
root /app/webroot;
index index.php;
try_files $uri /api/index.php$is_args$args;
location ~ \.php$ {
fastcgi_pass fastcgi_backend;
fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME /app/webroot/$fastcgi_script_name;
include fastcgi_params;
}
}
}
}
I just want to make it more simple and efficient, because as I see it now it's a mess.
I tried to adjust for example
try_files $uri /api/index.php$is_args$args;
to
try_files $uri /api/webroot/index.php$is_args$args;
and it failed... The only reason that it works is that /api/index.php includes /api/webroot/index.php, but I see it's inefficient.
I found debugging nginx config hard, because it's not easy to test.
Thank you very much for your help in advance!
The simplest solution would be to hardwire SCRIPT_FILENAME with a value of /app/webroot/index.php and remove one of your location blocks altogether.
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
location ~ ^/(api|api-debug)/ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot/index.php;
fastcgi_pass fastcgi_backend;
}
Alternatively, to keep the flexibility of specifying a URI with a .php extension, you could simplify the configuration with:
location / {
root /usr/share/nginx/html;
index index.html index.htm;
rewrite ^/(api|api-debug)/ /index.php last;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /app/webroot$uri;
fastcgi_pass fastcgi_backend;
}

Nginx to serve php files from a different server

I am trying to configure nginx to serve PHP from another server.
The files can be located within a directory under /sample on the other server
Fast CGI is running on port 9000 on the other server
Here is what I have tried, which is not working at the moment.
location ~ [^/]\.php(/|$) {
proxy_pass http://192.168.x.xx;
proxy_redirect http://192.168.x.xx /sample;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name)
{
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_read_timeout 150;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
I also need to configure nginx to serve static files from the same server
The following configuration does exactly what you need:
server {
listen 80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root {STATIC-FILES-LOCATION};
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass {PHP-FPM-SERVER}:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
All you have to do is replace {STATIC-FILES-LOCATION} with the location of your static files on the Nginx server and {PHP-FPM-SERVER} with the IP of the PHP-FPM server.
This way you will serve all files without the PHP extension statically from the Nginx server and all the PHP files will be interpreted with the PHP-FPM server.
Here's a working example of a dockerised version of what you are trying to achieve - https://github.com/mikechernev/dockerised-php/. It serves the static files from Nginx and interprets the PHP files via the PHP-FPM container. In the accompanying blog post (http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/) I go in lengths about the whole connection between Nginx and PHP-FPM.
EDIT: One important thing to keep in mind is that the paths in both the Nginx and PHP-FPM servers should match. So you will have to put your php files in the same directory on the PHP-FPM server as your static files on the Nginx one ({STATIC-FILES-LOCATION}).
An example would be to have /var/www/ on Nginx holding your static files and /var/www on PHP-FPM to hold your php files.
Hope this helps :)
You don't have to use proxy_ directives, because they work with HTTP protocol, but in this case FastCGI protocol is used. Also, as it was said in comments, no need for if statement, because Nginx server cannot determine if the file on a remote server exists.
You could try this configuration:
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_read_timeout 150;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_busy_buffers_size 256k;
fastcgi_pass 192.168.x.xx:9000; #not 127.0.0.1, because we must send request to remote PHP-FPM server
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /path/to/site/root$fastcgi_script_name;
}
You will need to replace /path/to/site/root with a real path on the PHP-FPM server. For example, if the request http://example.com/some/file.php must be handled by /var/www/some/file.php, then set it like this:
fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
Also, to make PHP-FPM server be able to receive requests from outside, edit your FPM pool configuration (on Debian it usually located in /etc/php5/fpm/pool.d/www.conf, on Centos - /etc/php-fpm.d/www.conf):
Replace
listen = 127.0.0.1:9000
with:
listen = 9000
or:
listen = 192.168.x.xx:9000 # FPM server IP
Probably you will also need to edit allowed_clients directive:
listen.allowed_clients = 127.0.0.1,192.168.y.yy # Nginx server IP
I also need to configure nginx to serve static files from the same server
If I understand correctly, and you want to serve static files from the server, Nginx is working on, then you may just add another location to your Nginx configuration.
You should not use proxy_* directives. using Nginx as a proxy would be done only if a distant server has rendered the page (and you would request it with HTTP protocol).
Here the thing you want to proxy is a fastcgi server, not an HTTP server.
So the key is:
fastcgi_pass 127.0.0.1:9000;
Where you currently say you want to reach a fastcgi server on IP 127.0.0.1 port 900, which seems quite wrong.
Use instead:
fastcgi_pass 192.168.x.xx:9000;
And remove proxy_* stuff.
Edit: also, as stated in comments by #Bart, you should not use an if testing that a local file in the document root, matching the php script name does exists. The php files are not on this server. So remove this file.
If you want to apply some security check, you would, later, alter your very generic location [^/]\.php(/|$) to something more specific, like location=/index\.php, or some others variations.
No need to give /sample path
location ~ [^/]\.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
# Mitigate https://httpoxy.org/ vulnerabilities
fastcgi_param HTTP_PROXY "";
fastcgi_pass IP:9000;
fastcgi_index index.php;
include fastcgi_params;
}
For static files from nginx server you need to use try_files for that.
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
// other CGI parameters
}
Make sure you're aware of common pitfalls.
If you want to access static files from another server you need to run a webserver there and just proxy pass from Nginx

Using Django and PHP in Nginx

I'm using Django in a Vhost and PHP in another, now I want to make one file with the two things, a part for Django and another for PHP.
I've read this question but it's not helping me.
How to run django and wordpress on NGINX server using same domain?
this one didn't help either
How can I configure nginx to serve a Django app and a Wordpress site?
This is part of what I have in the PHP config:
root /var/www/agendav/web/public;
# Add index.php to the list if you are using PHP
index index.php;
location ~ ^(.+\.php)(.*)$ {
try_files $fastcgi_script_name =404;
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
This is the full Django part:
upstream django {
server unix:///tmp/mysite.sock;
}
root /path/to/project/folder;
charset utf-8;
index index.html index.htm index.php;
if ($allowed_country = no) {
return 444;
}
# max upload size
client_max_body_size 75M;
# Django media
location /media {
alias /path/to/mysite/media/;
}
location /static {
alias /path/to/mysite/static/;
}
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass pass;
include /path/to/uwsgi_params;
}
location /.well-known/acme-challenge/ {
allow all;
}
And I'm trying to add this to the other conf file (that's already working)
location ~ /cal/.*\.php$ {
root /var/www/agendav/web/public;
index index.php;
try_files $uri $uri/ /index.php/login =404;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
}
for what I've read, that should do, but all I've getting is a 404 error, or a blank page and no readings in the nginx log or even in the agendav(the app in PHP) ones.
What am I doing wrong?

Nginx doesn't find Symfony2 routes

I try to run a simple Symfony2 application on a virtual machine. I have Nginx on this VM (generated with PuPHPet and Vagrant), but there's a problem.
When I try to access to the demo route /demo (local.dev/Symfony/web/app_dev.php/demo), Nginx force me to download a file. It seems it works the same way when I specify a random route.
I don't know how to solve this problem, I searched while 2 days for a specific configuration of Nginx but I found nothing. Here is my config file for this Symfony app :
server {
server_name test.dev;
root /var/www/local.dev/Symfony/web;
location / {
try_files $uri /app.php$is_args$args;
}
location ~ ^/(app|app_dev|config)\.php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_log /var/log/nginx/sf-error.log;
access_log /var/log/nginx/sf-access.log;
}
Thanks for your help.

Categories