PHP assets not being served by nginx - php

I've installed a WP blog and added a location block for it on the nginx server block configuration. When visiting the blog at example.com/blog or any of the blog posts like example.com/blog/foo-bar-baz the content is served but all assets are missing.
When I check the logs it looks like all requests to those asset URLs are being permanently redirected to the root blog URL. Here's an example of a request from nginx's access log:
"GET /blog/wp-includes/js/jquery.min.js HTTP/1.1" 301 5 "https://example.com/blog/" "USER_AGENT_STRING"
Below is the nginx configuration:
server {
listen 80;
server_name www.example.com example.com;
return 301 https://www.example.com$request_uri;
}
server {
listen 443 ssl;
server_name example.com www.example.com;
root /home/example/current/public;
index index.html index.htm;
location ~ /blog {
index index.php index.html index.htm;
fastcgi_param SCRIPT_FILENAME /var/www/blog/index.php;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 256 128k;
fastcgi_connect_timeout 300s;
fastcgi_send_timeout 300s;
fastcgi_read_timeout 300s;
include fastcgi_params;
}
}
How can I edit either of the location blocks to have nginx catch the requests for all URLs that contain either /blog/wp-content/ or /blog/wp-includes/? I've tried adding the following location block but it did not work:
location ~ ^(/wp-content/|/wp-includes/)/.*\.(jpe?g|gif|css|png|js|ico|pdf|m4a|mov|mp3)$ {
root /var/www/blog;
}

Your configuration does not appear to have the ability to serve static content. A more conventional approach would be to use a nested location to serve the dynamic content. For example:
location ^~ /blog {
root /var/www;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php;
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
See this document for more.

Related

Laravel, Docker and Nginx: 404 for all files in /public folder

I created a new project in Laravel 7 by using docker-compose 3.3 and Nginx 1.17.
The enpoints created on Laravel works well, the problem comes when I try to access to whatever static asset in the '/public' folder. I tried with .css and .js files and also with the robots.txt but Laravel returns a 404 not found error.
This is my nginx.conf (taken from https://laravel.com/docs/7.x/deployment#nginx)
events {
}
http {
server {
server {
listen 80;
server_name localhost;
root /var/www/app/public;
index index.html index.htm index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass app:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
}
I tried different fixes found on forums and on Stackoverflow, like recompiling files, clear the cache... But nothing works.
Can you spot the problem?
can you try the following config:
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 /var/www/html/public;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
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;
}
}

How to make a server block configuration file to run a CakePHP app on Nginx server?

Since I'm new to DigitalOcean and to Nginx server I don't have an idea what I am doing exactly
Having said that here's issue I'm getting
I've put the CakeApp folder inside /var/www/html folder and also I added an info.php page into webroot folder of my CakeApp to check if I can access it
When I go to http://my_ip/CakeApp it redirects to http://my_ip/CakeApp/login page and give a 404, but when I access the info.php file by going to http://my_ip/CakeApp/info.php it works and returns the PHP info page
here is the sever block file
server {
listen 80;
listen [::]:80;
server_name app.cake.com;
return 301 http://app.cake.com$request_uri;
root /var/www/html/CakeApp/public/webroot;
index index.php;
location /CakeApp/webroot {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
}
So I want to get the login page when I go to http://my_ip/CakeApp
Found a fix.
server {
listen 80;
root /var/www/html/CakeApp/webroot;
index index.php index.html index.htm index.nginx-debian.html;
server_name example.com;
location / {
rewrite ^(.+)$ $1 break;
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}

wordpress nginx rewrite to wp-admin

I want to create dockerized Wordpress with nginx that will use /kokpit instead of /wp-admin I'm using nginx:latest container with default config. And declared my server block for Wordpress like below
server {
listen 443 ssl;
ssl_certificate /etc/nginx/localhost.crt;
ssl_certificate_key /etc/nginx/localhost.key;
server_name dimidia.dev.com;
root /var/www/html;
index index.php;
error_log /var/log/nginx/stderr_dimidia debug;
access_log /var/log/nginx/stdout_dimidia;
location /media
{
rewrite ^/media/(.+) /wp-content/themes/mytheme/$1;
}
location /
{
try_files $uri $uri/ /index.php?$args;
}
location /kokpit
{
rewrite ^/kokpit(.*) /wp-admin$1;
}
location ~ \.php$ {
fastcgi_read_timeout 300;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
client_max_body_size 50m;
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
fastcgi_pass dimidia_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;
}
}
However when I reach to /kokpit files like load-scripts.php are returning 404 instead of scripts, and I cannot reach any php files inside kokpit (all are returning 404) big question is: why?

Run multiple path under nginx server in local machine, 502 bad gateway when use index.php file as default page

I try to setup a website run with different path into nginx.conf, I have 3 locations testing under the nginx conf, one is a node.js web app, one is a php forum, and the last one is phpmyadmin and it works fine. The problem is on my forum app. the index.php file shows 502 bad gateway when I test it, however it works when I use index.html. Below is configuration file, any idea about this setting?
nginx.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://nodejs;
}
location /forum {
root /parent/path/of/the/folder/;
index index.php index.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
location /phpmyadmin3 {
root /usr/local/share;
index index.php;
# try_files $uri $uri/ /phpmyadmin/index.php$args$is_args;
location ~ \.php$ {
fastcgi_pass unix:/path/to/.valet/valet.sock;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
UPDATE
nginx.conf
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://nodejs;
}
location /forum {
root /parent/path/of/the/folder/;
index index.php index.html;
}
location /phpmyadmin3 {
root /usr/local/share;
index index.php;
# try_files $uri $uri/ /phpmyadmin/index.php$args$is_args;
}
location ~ \.php$ {
fastcgi_pass unix:/path/to/.valet/valet.sock;
include fastcgi.conf;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

NGINX FASTCGI/PHP - Primary script unknown while reading response header from upstream, client

I have a development box that each user has a www folder in the home dir. NGINX is hosting those dirs from http://IP ADDRESS/USERNAME. And this works great. I want to get PHP working in the same fashion.
/etc/nginx/sites-available-default:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
#root /usr/share/nginx/html;
#index index.html index.htm;
# Make site accessible from http://IP ADDRESS/
server_name IP ADDRESS;
#location ~ ^/(.+?)(/.*)?$ {
location ~ ^/(.+?)(/.*)?$ {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
alias /home/$1/www$2;
index index.html index.htm index.php;
include /etc/nginx/php.fast.conf;
autoindex on;
}
php.fast.conf file:
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)?$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME /home/$1/www$2$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
As you can see I have tried a few variations but seem to continue to receive the following error in the log: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client.
When attempting to render a simple PHP info page The page displays "file not found"
Other info:
Server = ubuntu 14.x
Latest Nginx
Digital Ocean Droplet
Thanks in advance.
server {
listen 80 default_server;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name _;
location ~* \.php$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location ~* ^/(.+?)/www(/.*|/|)$ {
root /home;
index index.php index.html;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
fastcgi_param PATH_INFO $path_info;
fastcgi_index index.php;
include fastcgi_params;
}
location ~* ^/(.+?)(/.*|/|)$ {
index index.php index.html;
try_files $uri $uri/ #home;
}
location #home {
rewrite ^/([^/]+?)$ /$1/www/ last;
rewrite ^/(.+?)(/.*|/)$ /$1/www$2 last;
}
}
The cons of this config, you can't use URI like /<anything_you_want>/www.
Maybe it helps. But it's weird, non-optimized and ugly config.

Categories