Caddy2 - Routing with php_fastcgi and path - php

I'm running a Caddy server with php on localhost
My server is running perfectly on the main path : http://x.x.x.x/index.php.
This is my initial configuration :
x.x.x.x:80 {
root * /var/www/html
php_fastcgi php:9000
file_server
}
If I try to serve my php over another path http://x.x.x.x/dashboard/index.php, it will always return "file not found" :
x.x.x.x:80 {
root * /var/www/html
php_fastcgi /dashboard/* php:9000
file_server
}
or :
x.x.x.x:80 {
route /dashboard/* {
root * /var/www/html
php_fastcgi php:9000
file_server
}
}
I don't really understand.

Related

NGINX Setup with WordPress Multisite

I wanted to know the configuration for WordPress Multisite when the main site is in the root and the others in /site-name/
For example:
Main website: http://www.example.com/ Second website: http://www.example.com/second-site/ Third site: http://www.example.com/thrid-site/
I looked for the setting, but I only found it with subdomains.
Let's say your folder structure is as follows:
/root_folder/*
/root_folder2/second-site/*
/root_folder3/third-site/*
Then you need:
server {
server_name www.example.com;
root /root_folder/;
location / {
# code
}
location /second-site {
root /root_folder2/;
# code
}
location /third-site {
root /root_folder3/;
# code
}
}
It's important not to add the /second-site and /third-site in the root directive as Nginx will automatically add requested subpaths to the root path at request.
If your folder structure is as follows:
/root_folder/*
/root_folder/second-site
/root_folder/third-site
You only need
server {
server_name www.example.com;
root /root_folder/;
location / {
# code
}
}
And Nginx will do the rest for you.

base config for docker-compose, caddy and php-fpm

I'm trying to build a base config with caddy and php-fpm on docker-compose. Problem is, I get a "404 file not found" when I try to reach for my index.php file. Here is my config.
docker-compose.yml
version: "3.8"
services:
caddy:
image: caddy:alpine
ports:
- "80:80"
- "443:443"
- "443:443/udp"
volumes:
- $PWD/Caddyfile:/etc/caddy/Caddyfile
- $PWD/www:/srv/www
- $PWD/caddy/data:/data
- $PWD/caddy/config:/config
- $PWD/caddy/log:/var/log
depends_on:
- app
app:
image: php:fpm-alpine
ports:
- "9000:9000"
volumes:
- "$PWD/www:/var/www/html"
Caddyfile
localhost:80 {
root * /srv/www
php_fastcgi app:9000
file_server
}
Finally I have a www folder containing index.php and test.html - http://localhost/test.html works, but http://localhost/index.php gives me a 404.
What am I doing wrong?
EDIT : here is what I tried :
I checked that I can ping from one container to the other
port 9000 is effectively opened on the php container
It looks like the php files are not mounted at the right place inside the php container, but /var/www/html is the WorkingDir.
I don't know where to go next to troubleshoot this.
Finally, I found the answer!
php-fpm needs the absolute path of the file, so you either have to have the same path in both containers or add a root directive inside the php_fastcgi block.
localhost:80 {
root * /srv/www
encode gzip
php_fastcgi php:9000 {
root /var/www/html
}
file_server
log
}

Ajax POST to php file from nodejs express server with nginx reverse proxy on raspberry pi

So I set up a nodejs web server using express, and a reverse proxy via nginx on my raspberry pi. I am a complete beginner to web servers and routing, so I am trying my best getting things to work. I first installed nginx via sudo apt-get install nginx. I also installed php7.4-fpm, and I edited the nginx default configuration with sudo nano /etc/nginx/sites-available/default so it would accept php files. I then created a php file named test.php in /var/www/html with the content <?php phpinfo(); ?>. So far everthing was fine, I could see the php working on the localhost. Then I wanted to install a nodejs webserver, and forward the requests from nginx to that nodejs webserver. For that webserver I am using express. My entry point index.js for that nodejs server looks as follows:
const express = require("express");
const path = require("path");
const app = express();
const port = process.env.PORT || 3000;
//make the localhost/public folder the default
app.use(express.static(path.join(__dirname, "public")));
//make the private folder accessible via localhost/private
app.use("/private", express.static(path.join(__dirname, "private")));
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "/public/index.html"));
});
app.listen(port, () => {
console.log("App listening on http://localhost:" + port);
});
My filestructure for that server is following:
server
|____ index.js
|____ package.json
|____ package-lock.json
|____ node_modules
|____ private
|____ scripts
|___ restart.php
|____ index.html
|____ index.js
|____ public
|____ index.html
Then I connected nginx and the nodejs server by editing /etc/nginx/nginx.conf to look like this:
events {}
http {
index index.html index.php;
default_type text/html;
server {
listen 80;
location / {
proxy_pass http://localhost:3000/;
}
}
}
Starting the webserver with node index.js everything works fine, I can see the index.html file from the public folder by default, but can also access the private directory. However, when trying to access the restart.php file by typing http://localhost/private/scripts/restart.php into the url field, the php file only gets downloaded instead of executed or showing the code. I originally tried to POST to the php file via AJAX, but I always get an 404 file not found error, even though I can get that file by accessing the url. For the AJAX call, my JavaScript looks like this:
function restart_server() {
let request = new XMLHttpRequest();
request.onload = function() {
if (this.status >= 200 && this.status < 400) {
console.log("success");
else {
console.log("error");
}
}
request.onerror = function () {
console.log("error");
}
request.open("POST", "restart.php", true);
request.send();
}
The restart_server function gets called when I click a button on the index.html page of the private directory.
I really dont know what I am doing wrong, I have tried everything on the internet I could find, but nothing worked. I am also confused by the 404 file not found error, because I can access the file, and the url in the AJAX POST should be correct. Many thanks and king regards.
Since you're proxying all requests to your NodeJS server, it is not able to run the PHP script. (The only handler at that point is express server and it only knows that it should serve these files as static files - no matter what it's type, so PHP is not something special here - but it should be)
You need to tell nginx to serve static files as static but *.php files via php-fpm.
You need something like that;
location ^~ /private {
root /www/server/private;
try_files $uri $uri/ /404.php$is_args$args;
location ~* \.php(/|$) {
fastcgi_pass php:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

Docker: can not communicate between containers

I have a Docker setup with a php-fpm container, a node container and an nginx container which serves as a proxy. Now in the browser (http://project.dev), the php container responds with json like I expect. All good. However, when I make a request from the node container to this php container (view code), I get an error on the request: ECONNRESET. So apparently, the node container can not communicate with the php container. The nginx error does not seem to add an entry.
Error: read ECONNRESET at _errnoException(util.js: 1031: 13) at TCP.onread(net.js: 619: 25)
errno: 'ECONNRESET',
code: 'ECONNRESET',
syscall: 'read'
Any ideas?
I've made a github repo: https://github.com/thomastilkema/docker-nginx-php-fpm-node
Trimmed version of docker-compose.yml (view file)
nginx:
depends_on:
- php-fpm
- node
networks:
- app
ports:
- 80:80
php-fpm:
networks:
- app
node:
networks:
- app
networks:
app:
driver: overlay
Trimmed version of nginx.conf (view file)
http {
upstream php-fpm {
server php-fpm:9000;
}
upstream node {
server node:4000;
}
server {
listen 80 reuseport;
server_name api.project.dev;
location ~ \.php$ {
fastcgi_pass php-fpm;
...
}
}
server {
listen 80;
server_name project.dev;
location / {
proxy_pass http://node;
}
}
}
php-fpm/Dockerfile (view file)
FROM php:7.1-fpm-alpine
WORKDIR /var/www
EXPOSE 9000
CMD ["php-fpm"]
Request which gives an error
const response = await axios.get('http://php-fpm:9000');
How to reproduce
Create a swarm manager (and a worker) node
Find out the ip address of your swarm manager node (usually 192.168.99.100): docker-machine ip manager or docker-machine ls. Edit your hosts file (on a Mac, sudo vi /private/etc/hosts) by adding 192.168.99.100 project.dev and 192.168.99.100 api.project.dev
git clone https://github.com/thomastilkema/docker-nginx-php-fpm-node project
cd project ./scripts/up.sh
Have a look at the logs of the container: docker logs <container-id> -f
ECONNRESET is the other end closing the connection which can usually be attributed to a protocol error.
The FastCGI Process Manager (FPM) uses the FastCGI protocol to transport data.
Go via the nginx container, which translates the HTTP request to FastCGI
axios.get('http://nginx/whatever.php')

Location of document root of public html in Centos

I am following installation steps for Drupal and I am unable to follow one of the steps given below.
Move the contents of the drupal-x.x directory into a directory within
your web server's document root or public HTML directory (ensure that
the .htaccess file, a hidden file, is successfully moved into the
destination directory as well.
I am not sure where web server's document root or public HTML directory are located in my Centos machine. I tried finding public_html directory in my Centos machine using
I tried the following:
sudo find / -name public_html -type d - it did not return anything.
sudo find / -name .htaccess - it did not return anything except the .htaccess file from the drupal download
Apache is installed though -
httpd -v: (returned the following on terminal)
Server version: Apache/2.2.15 (Unix)
Server built: May 23 2014 14:34:17
The default is /var/www/html but this can be set to any directory in the /etc/httpd/conf/httpd.conf file.

Categories