I have two containers:
laravel with php:8.1-fpm-bullseye container exposed on 9000
nginx with nginx:1.23.1
nginx uses fastcgi_pass directive in configuration file
Question:
why do i need to also mount my app source code to nginx /var/www/html directory when all nginx does is that it turns http request to fastcgi request and passes them to laravel container? ( I commented out try_files directive in configuration but I'm still getting 404 response from nginx)
./nginx/nginx-default.conf:
upstream backend {
server laravel:9000;
}
server {
listen 80;
listen [::]:80;
server_name _;
error_log /var/log/nginx/error80.log;
access_log /var/log/nginx/access80.log custom;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
listen [::]:443 ssl;
ssl_certificate /etc/ssl/certs/localhost.crt;
ssl_certificate_key /etc/ssl/private/localhost.key;
root /var/www/html/public;
error_log /var/log/nginx/error443.log;
access_log /var/log/nginx/access443.log custom;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ [^/]\.php(/|$) {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_keep_conn on;
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param REDIRECT_STATUS 200;
}
}
./docker-compose.yml:
version: "3.8"
services:
user:
container_name: user
build:
context: ./
dockerfile: Dockerfile.development
volumes:
- ./:/var/www/html:rw
networks:
- user
user-nginx:
container_name: user-nginx
build:
context: ./nginx
dockerfile: Dockerfile.development
volumes:
# - ./:/var/www/html:rw # here !
- ./nginx/nginx-default.conf:/etc/nginx/conf.d/default.conf:rw
- ./nginx/nginx.conf:/etc/nginx/nginx.conf:rw
- ./nginx/logs:/var/log/nginx:rw
networks:
- user
depends_on:
- user
networks:
user:
driver: bridge
./nginx/Dockerfile.development:
FROM nginx:1.23.3
USER root
RUN usermod -aG root nginx
EXPOSE 80 443
Related
I am running 2 CodeIgniter instances (admin+public) in Docker+Nginx+Php.
When I open the admin website on my browser, the file /admin/application/logs/log-2023-01-23.php shows me an error log :
ERROR - 2023-01-23 06:27:00 --> 404 Page Not Found: Resources/fonts
ERROR - 2023-01-23 06:27:00 --> 404 Page Not Found: Resources/css
ERROR - 2023-01-23 06:27:04 --> 404 Page Not Found: Faviconico/index
My questions :
Why are all folders and subfolders in /admin/resources not recognised ?
Is there a way to have 2 subdomains admin.domain.com and public.domain.com serving the folder admin and public ?
The tree is like this :
-admin
--application
--resources
--system
--*index.php
-public
--application
--resources
--system
-sql
-*docker-compose
-*Dockerfile
-*site.conf
This is how the docker-compose.yml file looks like
version: '3'
services:
nginxall:
depends_on:
- database
image: nginx:latest
volumes:
- ./:/public
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
codeigniter_net:
ports:
- "80:80"
restart: always
php:
build:
context: ./
dockerfile: Dockerfile
volumes:
- ./public:/public
- ./admin:/admin
depends_on:
- database
networks:
codeigniter_net:
database:
image: mysql:5.7
volumes:
- ./sql/a3inf4qq_kilifair.sql:/docker-entrypoint-initdb.d/a3inf4qq_kilifair.sql
networks:
codeigniter_net:
ports:
- 3306:3306
env_file:
- .env
networks:
codeigniter_net:
This is how the site.conf looks like
server {
server_name admin-site.com ;
listen 80;
index index.php index.html;
root /admin;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# set client body size#
client_max_body_size 8M;
location / {
try_files $uri /index.php?$args ;
proxy_pass http://admin-site.com:82;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /admin$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(jpe?g|gif|png|bmp|ico|css|js|pdf|zip|htm|html|docx?|xlsx?|pptx?|txt|wav|swf|avi|mp\d)$ {
access_log off;
log_not_found off;
try_files $uri $uri/ /admin/$uri /index.php?$args ;
expires 1w;
}
}
server {
server_name public-site.com ;
listen 80 ;
index index.php index.html;
root /public;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# set client body size#
client_max_body_size 8M;
location / {
try_files $uri /index.php?$args ;
proxy_pass http://public-site.com:81;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /public$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location ~* \.(jpe?g|gif|png|bmp|ico|css|js|pdf|zip|htm|html|docx?|xlsx?|pptx?|txt|wav|swf|avi|mp\d)$ {
access_log off;
log_not_found off;
try_files $uri $uri/ /public/$uri /index.php?$args ;
expires 1w;
}
}
There was a problem with my docker compose nginx container
I forgot to add the folder admin in nginx.volumes
nginxall:
depends_on:
- database
image: nginx:latest
volumes:
- ./admin:/admin
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/default.conf
networks:
codeigniter_net:
ports:
- "80:80"
restart: always
I have set up a docker-compose.yml file to start a NGiNX server and serve PHP content, the thing is it keeps on showing the default NGiNX welcome page when I visit localhost:8080.
Here's my docker-compose.yml
version: "3"
services:
web:
image: nginx:latest
ports:
- "8080:80"
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/site.conf
links:
- php
php:
image: php:8-fpm
volumes:
- ./public:/public
And here's my site.conf
server {
index index.php index.html;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /public;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
If you have an index.php file inside the public folder, you just need to change the docker-compose.yml to:
volumes:
- ./public:/public
- ./site.conf:/etc/nginx/conf.d/default.conf
I have got this error when I tried to run docker-compose up :
Error: host not found in upstream "php-fpm" in /etc/nginx/conf.d/default.conf
Below is the code which I am using in docker-compose.yml.
docker-composer version: '3.7'
services:
ngnix:
build:
args:
VERSION: $NGINX_VERSION
context: .
dockerfile: ./docker/nginx/Dockerfile
target: dev
volumes:
- .:/app
depends_on:
- php-fpm
links:
- php-fpm
ports:
- 81:80
networks:
- inluccnetwork
php-fpm:
build:
args:
VERSION: $PHP_VERSION
context: .
dockerfile: ./docker/php-fpm/Dockerfile
target: dev
volumes:
- .:/app
networks :
- inluccnetwork
command: sh -c 'composer install --no-interaction --optimize-autoloader && php-fpm'
networks:
inluccnetwork :
driver: bridge
Nginx config file code at /etc/nginx/conf.d/default.conf:
server {
listen 80;
root /app/web;
include mime.types;
index app_dev.php index.html index.htm;
location / {
try_files $uri #rewriteapp;
}
location #rewriteapp {
rewrite ^(.*)$ /app_dev.php/$1 last;
}
# DEV
# This rule should only be placed on your development environment
# In production, don't include this and don't deploy app_dev.php or config.php
location ~ ^/(app_dev|config)\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
internal;
}
# PROD
location ~ ^/app\.php(/|$) {
fastcgi_pass php-fpm:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $document_root;
internal;
}
# return 404 for all other php files not matching the front controller
# this prevents access to other php files you don't want to be accessible.
location ~ \.php$ {
return 404;
}
location /test {
return 200 '<h1>ok</h1>';
}
error_log syslog;
access_log syslog;
location ~ /\.ht {
deny all;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc)$ {
access_log off;
root /app/web;
add_header Cache-Control "max-age=2592000";
expires max;
log_not_found off;
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
add_header Cache-Control "max-age=31536000";
access_log off;
expires max;
log_not_found off;
}
}
Code I am using in my docker file for Nginx:
ARG VERSION
# Dev image
FROM nginx:1.15-alpine as dev
# Copy nginx config
COPY ./docker/nginx/default.conf /etc/nginx/conf.d/default.conf
# Prod image
FROM dev as prod
# Copy assets
COPY ./assets /app/web
I have error not know php-fpm in file default.php and also I add option links in docker-compose but also error
I'm attempting to setup a development environment using docker using 2 containers: nginx and php7-fpm.
What I want to happen is when a user visits any URL that contains /api it uses php-fpm, but everything else is loaded from /var/www/html.
Here is my configuration:
site.conf:
server {
index index.html;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location /api {
index index.php;
alias /var/www/api;
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
}
}
docker-compose.yml
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
links: [ php ]
ports:
- "8080:80"
environment:
- NGINX_HOST=http://impressive.local
- NGINX_PORT=80
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
This doesn't work as expected and when I visit impressive.local/api I get the following error in logs:
web_1 | 2019/01/10 12:23:47 [error] 6#6: *1 "/var/www/api/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: impressive.local, request: "GET /api/ HTTP/1.1", host: "impressive.local:8080"
I realize that the php-fpm container is the one that contains the /var/www/api directory and not nginx. With my configuration nginx is trying to alias to a non existent path and is thus failing.
My question is how is possible to achieve this?
Yes, I use exactly this configuration for all of my Laravel apps.
Here is an example of my configuration...
version: '2'
services:
app:
container_name: app
build:
context: ./
dockerfile: app.dockerfile
working_dir: /var/www
volumes:
- ./:/var/www
environment:
- "DB_PORT=3306"
- "DB_HOST=x.x.x.x"
web:
container_name: web
build:
context: ./
dockerfile: web.dockerfile
working_dir: /var/www
volumes_from:
- app
ports:
- 8080:80
As you can see you specify to use the volume from your web container.
I think the configuration file is invalid; try this code to fix [ docker-compose.yml and site.conf ]
docker-compose.yml
version: '2'
services:
web:
image: nginx
volumes:
- ./frontend/public:/var/www/html
- ./site.conf:/etc/nginx/conf.d/site.conf
ports:
- "8080:80"
environment:
- NGINX_HOST=impressive.local
- NGINX_PORT=80
links:
- php
php:
image: php:7-fpm
volumes:
- ./api:/var/www/api
- ./frontend/public:/var/www/html
site.conf
server {
index index.html index.php;
server_name impressive.local;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root /var/www/html;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /api/ {
alias /var/www/api;
}
location ~ ^/api/(.+\.php)$ {
alias /var/www/api;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# pass the PHP scripts to FastCGI server listening on php:9000
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
finally, run docker-compose build and docker-compose up
I'm working with containers in docker
Where I have one from PHP-FPM and another from Nginx.
But I'm having problems with Nginx to serve the static files (css, js)
Return Status Code: 404 Not Found
Nginx configuration
server {
# Set the port to listen on and the server name
listen 80;
listen [::]:80;
# Set the document root of the project
root /var/www/html;
# Set the directory index files
index index.php;
#Set server name
server_name myproject;
# Specify the default character set
charset utf-8;
# Specify the logging configuration
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Specify what happens when PHP files are requested
location ~* \.php$ {
#try_files $uri =404;
#try_files /index.php = 404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass myproject:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
location / {
index index.php;
try_files $uri $uri/ /index.php;
include /etc/nginx/mime.types;
}
location ~* \.(jpg|jpeg|png|css|js|ico|xml)$ {
access_log off;
log_not_found off;
expires 360d;
add_header Cache-Control "public";
}
# Specify what happens what .ht files are requested
location ~ /\.ht {
deny all;
}
}
PHP Dockerfile
FROM php:7-fpm
RUN docker-php-ext-install pdo_mysql
COPY . /var/www/html/
EXPOSE 9000
Nginx Dockerfile
FROM nginx:1.12.2
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
I think the problem, cause service nginx can not find your web project. If you use docker-compose you can use volume, but if not you can add folder project in nginx Dockerfile to /var/www/html
nginx dockerfile
ROM nginx:1.12.2
COPY . /var/www/html/
COPY ./default.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
for docker compose like this :
services:
nginx:
images: nginx:latest
...
...
volumes:
- ./:/var/www/html
php:
images: php
...
...
volumes:
- ./:/var/www/html