NGINX Cannot Find PHP Files - php

When I go to an address belonging to the .php extension, it gives an error and does not open the relevant file.
For example:
When I open the https://www.example.com/test.php file, it gives 404 error.
But
When I open the https://www.example.com/test.txt file it opens successfully.
Note:I am using Load Balance.
NGINX site configuration settings:
proxy_cache_path /tmp/cacheapi/examplecomm levels=1:2 keys_zone=examplecomm_microcacheapi:100m max_size=1g inactive=1d use_temp_path=off;
server {
proxy_cache examplecomm_microcacheapi;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 302 30s;
if ($host = example.com) {
return 301 https://www.$host$request_uri;
}
root /var/www/xxx/public_html/www/eshop;
include snippets/php8.conf;
access_log /var/www/xxx/public_html/www/logs/examplecomm.acces.log;
error_log /var/www/xxx/public_html/www/logs/examplecomm.error.log;
error_page 404 /404;
error_page 301 /;
location ~* \.(css|js|pdf|zip|rar|txt|webp)$ {
expires 1h;
}
# Add index.php to the list if you are using PHP
index index.html index.htm index.php;
server_name example.com www.example.com;
location / {
rewrite ^/([a-z,_,A-Z,0-9,\/,-]+)/$ /index.php?page=$1&$query_string;
rewrite ^/([a-z,_,A-Z,0-9,\/,-]+)$ /index.php?page=$1&$query_string;
}
listen [::]:80;
listen 80;
}
snippets/php8.conf:
location ~ \.php$ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $host;
proxy_cache_valid 3s;
proxy_no_cache $cookie_PHPSESSID;
proxy_cache_bypass $cookie_PHPSESSID;
proxy_cache_key "$scheme$host$request_uri";
add_header X-Cache $upstream_cache_status;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
include fastcgi_params;
}
Could you help me?

Related

Nginx Reverse Proxy returns 404 for PHP

I am running an Nginx server and several services in jails. I have two TLDs, one old and one new. For the new TLD I have added a new jail with a new service (wordpress). I added a new server block to my reverse proxy. Accessed locally, bypassing the reverse proxy, wordpress works fine. All PHP executes correctly.
However, accessed through the reverse proxy, using the new TLD, any attempt to navigate to a .php file returns a 404 error. Note that the site itself is working and php is properly executing; the issue only arises if you try to navigate to a .php directly. This is problematic, for example, because you can't access the login page. In fact, you can't even navigate to index.php, even though going to domain2.com itself works, domain2.com/index.php fails.
These are the server blocks from my nginx.conf:
#Domain2
server {
server_tokens off;
listen 80;
server_name www.domain2.com domain2.com;
return 301 https://$host$request_uri;
}
server {
server_tokens off;
listen 443 ssl;
server_name www.domain2.com domain2.com;
ssl_certificate /usr/local/etc/letsencrypt/live/domain2.com/cert.pem;
ssl_certificate_key /usr/local/etc/letsencrypt/live/domain2.com/privkey.pem;
#USE SECURE PROTOCOLS
ssl_protocols TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;
ssl_ciphers "EECDH+AESGCM:EDH+AESGCM:ECDHE-RSA-AES128-GCM-SHA256:AES256+EECDH:DHE-RSA-AES128-GCM-SHA256:AES256+EDH:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4";
add_header Strict-Transport-Security "max-age=63072000; includeSubdomains; preload";
#DEFINE ACCESS LOG LOCATION
access_log /var/log/nginx/access_domain2.log;
#PASS PHP TO FASTCGI
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
#PROXY_SETTINGS
client_max_body_size 10m;
client_body_buffer_size 128k;
#Timeout if the real server is dead
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
# Advanced Proxy Config
send_timeout 5m;
proxy_read_timeout 240;
proxy_send_timeout 240;
proxy_connect_timeout 240;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_redirect off;
proxy_http_version 1.1;
proxy_pass http://192.168.1.253;
}
error_page 401 403 404 /404.html;
#redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/www/nginx-dist;
}
}
I fixed this by commenting out the "pass php to fastcgi" which was apparently redundant.

Nginx proxy PHP-FPM + Lighttpd 405 not allowed symfony

Hello I am trying to serve a php web application using Nginx PHP-FPM using a tcp socket on a remote host and a lighttd to serve static content on a remote server.
I succeed to link those three blocks but I have an issue to manage the interactions.
Here my Nginx proxy configuration
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
When I removed error_page 405 = 200$uri; symfony is returning me a method post not allowed and when I put it nginx is returning me a 405.
I clearly missunderstand how the communication are established but don't know where I am wrong.
You are using auth_basic and auth_basic_user_file in the location / block. Am I right that you don't get a prompt to enter a password when you navigate to the side and get instead directly the 405 error?
I interpret the error to mean that an authentication is expected but doesn't happen. I would suggest to move auth_basic and auth_basic_user_file up one level in the server block. That way everything is covered and not only the one location block and you should get the password prompt.
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
I hope that helps you.

Nginx multiple listeners (with php application)

I want Nginx to serve both my jenkins server and my Laravel (php) application. Here is my Nginx configuration:
server {
listen 80 default_server;
server_name localhost;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:8081;
proxy_read_timeout 90;
proxy_redirect http://127.0.0.1:8081 https://jenkins.domain.tld;
}
}
server {
listen 9000;
listen [::]:9000;
root /var/www/my-app/public;
index index.php index.html index.htm;
server_name my-app.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using virtual-box as my virtual machine emulator for ubuntu 14. I am able to access jenkins application by going to http://127.0.0.1:8081 (I have changed the jenkins default port).
But when I am trying to access http://127.0.0.1:9000 I am getting an error, I have checked Ngnix logs but couldn't find any information regarding a request to port 9000.
Also I have made port forwarding in virtual-box, here is a screenshot:
Any help would be great.
Use server_name localhost too for php.

socket.io only works locally with nginx

(PHP runs with nginx, and I use socket.io from NODEJS)
If I try my website locally (using 2 web diffenrets web browzers), everything works.
But If I host my website (hosted in my house), I still can access to it with an other computer, but the functions of my app.js are not executed...
Here is my last error.log from nginx :
2016/05/03 14:11:00 [error] 25016#25016: *108 FastCGI sent in stderr: "PHP message: PHP Notice: Only variables should be passed by reference in /var/www/html/outer_treatment/game/add_message_discussion.php on line 55" while reading response header from upstream, client: 192.168.1.16, server: default, request: "POST /outer_treatment/game/add_message_discussion.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.1.13", referrer: "http://192.168.1.13/game.php"
in my page where the NodeJS functions are handled: (located in : /view/game/index.php)
# into the <head>
<script src="/NODEJS/socket.io/socket.io.js"></script>
# into the <body>
var socket = io.connect('127.0.0.1:3000');
And my nodeJs file app.js : (located in : /NODEJS/app.js)
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
fs = require('fs');
io.sockets.on('connection', function(socket)
{
// here my functions
});
server.listen(3000, "127.0.0.1");
Here is my default file of Nginx (located in : /etc/nginx/sites-available)
# the IP(s) on which node server is running.
upstream app_default {
server 127.0.0.1:3000;
keepalive 8;
}
server {
# Default listen lines :
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
# NODE JS listen
#listen 0.0.0.0:80;
listen 80;
#root /usr/share/nginx/html;
root /var/www/html;
index index.php index.html index.htm;
#server_name localhost;
server_name default;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules;
#NODEJS configuration :
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
# the websockets :
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location ~ \.png$ {
try_files $uri $uri /$1;
}
}
Thanks you, I hope my problem is enough accurate
I finally resolved my problem, in the index.php file I ve modified
var socket = io.connect('127.0.0.1:3000');
by
var socket = io.connect('http://'+window.location.host+':3000');
everything works now ! :)

Wordpress running on nginx on a subdomain doesn't work on port 80

I have a server hosting two Django websites and a wordpress blog, all on nginx. I have setup one Django site to be served at the top level domain, and the other at a subdomain.
I have not been able to get the Wordpress site to work when it is on port 80. If I change the port to 8079, it works correctly.
Here is my nginx conf for the Wordpress site:
server {
listen 80;
root /var/www;
index index.php index.html index.htm;
server_name sub2.mydomain.com;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/www;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri =404;
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
One thing I have noticed is that even after I change the port back to 80 and restart nginx, when I go to the url in my web browser, it will redirect to port 8079. I'm not sure if that is caused by the browser cache, or DNS or something on my server.
Here are my other nginx configs:
upstream mydomain_app_server {
server unix:/webapps/mydomain/run/gunicorn.sock fail_timeout=0;
}
server {
listen 80;
server_name www.mydomain.com;
client_max_body_size 4G;
access_log /webapps/mydomain/logs/nginx-access.log;
error_log /webapps/mydomain/logs/nginx-error.log;
location /static/ {
alias /webapps/mydomain/static/;
}
location /media/ {
alias /webapps/mydomain/media/;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://mydomain_app_server;
break;
}
}
# Error pages
error_page 500 502 503 504 /500.html;
location = /500.html {
root /webapps/mydomain/static/;
}
}
and my last one is identical except with different paths and the following server_name:
server_name sub2.mydomain.com;

Categories