I have a NodeJS app deployed in the root of my domain (example.com) and I'm struggling for the last 7 days to add a blog in a subdirectory (example.com/blog). Even though I have set it up but I think there is some issue with Nginx config as I can't access the blog posts (blog homepage is accessible). It is also worth mentioning that I CAN access the posts if I set Permalinks to Plain in Wordpress settings, which I don't want because of SEO.
If I look at error logs then I can see this:
2022/09/02 15:43:43 [error] 86838#86838: *6 "/var/www/html/example.com/blog/advantages-of-social-media-2/index.php" is not found (2: No such file or directory), client: 142.52.23.144, server: www.example.com, request: "GET /blog/advantages-of-social-media-2/ HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/blog/"
Wordpress installation is located in /var/www/html/example.com/blog but in the config I have not added /blog but I can still access the blog homepage. And if I add it, then I can't access it anymore.
I tried like a million different solutions but nothing seems to be working. Someone please help me out.
Here's the full Nginx config without the SSL statements:
server {
server_name example.com;
return 301 https://www.example.com$request_uri;
}
server {
server_name www.example.com;
# NodeJS App
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_set_header X-Real-IP $http_cf_connecting_ip;
proxy_set_header X-Forwarded-For $http_x_forwarded_for;
proxy_cache_bypass $http_upgrade;
}
# Wordpress Blog
location /blog {
access_log /var/log/nginx/blog_access.log;
error_log /var/log/nginx/blog_error.log;
root /var/www/html/example.com;
index index.php;
# Add a trailing slash if missing
if (!-f $request_filename) {
rewrite [^/]$ $uri/ permanent;
}
# try_files $uri $uri/ /index.php?$args;
# try_files $uri $uri/ /blog/index.php?q=$uri&$args;
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_index index.php;
# Change this to your fpm socket
fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
}
}
Try this, dont just copy and paste this and try to understand what each block do
server {
listen 443 ssl http2;
server_name www.example.com example.com;
#Addition Configs
.
.
.
.
.
error_log /var/www/html/example.com/error error;
root /var/www/html/example.com;
index index.html index.php;
location / {
gzip_static on;
error_page 418 = #cachemiss;
.
.
.
}
location #cachemiss {
try_files $uri $uri/ /index.php$is_args$args;
}
### DISABLE LOGGING ###
location = /robots.txt { access_log off; log_not_found off; }
location = /favicon.ico { access_log off; log_not_found off; }
### CACHES ###
location ~* \.(jpg|jpeg|gif|css|png|js|ico|html|mp4)$ { access_log off; expires max; }
location ~* \.(woff|svg|woff2|ttf|eot)$ { access_log off; log_not_found off; expires 30d; }
location ~* \.(js)$ { access_log off; log_not_found off; expires 7d; }
location /blog {
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
}
### php block ###
location ~ \.php?$ {
fastcgi_cache phpcache;
fastcgi_cache_valid 200 30m;
fastcgi_cache_methods GET HEAD;
try_files $uri =404;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_hide_header X-Powered-By;
fastcgi_pass 127.0.0.1:9000;
}
}
The most important part you have to configure is the php-block/fast-cgi location block which you should directly put under server block and outside the blog location
Then on your blog location just do a request to /blog/index.php
like so;
try_files $uri $uri/ /blog/index.php?q=$uri&$args;
your directory structure should be;
/var/www/html/example.com - your root folder which should contains any app you want
/var/www/html/example.com/blog - contains your wordpress installation
for fastcgi caching, you either remove this line
fastcgi_cache phpcache; from php block,
or go to /etc/nginx/nginx.conf and configure fastcgi caching like below;
the code below should be in http block
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=phpcache:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
Related
I have a nginx block for a wordpress installation, and a subdirectory which redirects to an app I have running. I want .php urls that go to the subdirectory to not be evaluated by fastcgi, since the app has it's own special system for evaluating them. However, php URLs still go to the fastcgi block and return a 404.
Nginx configuration:
server {
listen 0.0.0.0:443 http2 default_server;
listen [::]:443 http2 default_server;
ssl on;
ssl_certificate /etc/nginx/certificate.pem;
ssl_certificate_key /etc/nginx/certificate.key;
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; allow all; }
location ~* \.(css|gif|ico|jpeg|jpg|js|png)$ {
expires max;
log_not_found off;
}
root /var/www/wordpress;
index index.html index.php;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location /my_app {
proxy_pass http://localhost:8081/;
}
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I want /index.php to be evaluated by fastcgi.
I want /my_app/index.php to be proxy_pass'd to my app.
I have been trying to resolve this for like a day now. I have checked out all similar questions and done the same thing but this does not seem to work.
I am currently running a node JS app on the server and now I intend to add laravel to it. But all routes return the laravel 404 error page.
Here is the config file.
server {
listen 80;
listen [::]:80 ipv6only=on default_server;
root /var/www/html/dlabs.cloud-File-Service/public;
index index.html index.htm index.php;
server_name dlabs.com.ng;
location /auth/ {
proxy_pass http://localhost:3000;
proxy_pass_header Server;
proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /file-service/ {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location = /favicon.ico {
access_log off; log_not_found off;
}
location = /robots.txt {
access_log off; log_not_found off;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {
expires 5d;
}
# deny access to . files, for security
#
location ~ /\. {
log_not_found off;
deny all;
}
}
PHP version is Version 7.3.
The /auth/ works perfectly.
add try_files $uri $uri/ /index.php$is_args$args;
to nginx.conf
in => /etc/nginx/nginx.conf
I have two problems but I think they're related. Subdirectories like /wp-admin, /blog return 404, consequently, permalinks do not work as they follow /blog/category1/page.php
My setup:
I have a server 192.168.1.4 running nginx. On another server, 192.168.1.1 I have an apache web server using virtualhosts which is hosting my wordpress site. The setup works fine without nginx, but when I turn nginx on I have a few issues.
Nginx will not work with permalinks. I have used default, so now its like: http://www.mywebsite.co.uk/?page_id=90 which works fine (as long as its not in subdirectory).
Everything in subdirectory (not in root) breaks. Including admin pages http://www.mywebsite.co.uk/wp-admin, or (before turning off permalinks) http://www.mywebsite.co.uk/blog. They all go to 404, specifically: 404 Not Found nginx/1.4.6 (Ubuntu)
Here is my config:
server {
server_name mywebsite.co.uk www.mywebsite.co.uk;
location / {
index index.php;
proxy_pass http://192.168.1.1$request_uri;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
try_files $uri $uri/ =404;
#try_files $uri $uri/ /index.php?$args; #not working, error: rewrute or internal redirection cycle while interally redirecting to index.php
}
}
For reference, here is my permalinks: /blog/%category%/%postname%/
Update
Tried adding this to my config:
server {
... config above ...
location /wp-admin/ {
index index.php
try_files $uri $uri/ /wp-admin/index.php?$args;
proxy_pass http://192.168.1.1$request_uri;
proxy_set_header Host $host;
}
}
this retrns an error in log:
rewrite or internal redirection cycle while internally redirecting to "/wp-admin/index.php", client: xxxxx, server: mydomain.co.uk`
You have bits of a working configuration. The purpose of nginx in your configuration, is to reverse proxy to the Apache server. The index and try_files are inappropriate in this case. Try:
server {
server_name mywebsite.co.uk www.mywebsite.co.uk;
location / {
proxy_pass http://192.168.1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Why you dont want to try wordpress without apache ?
working config would be like this :
server {
listen 80;
server_name site.com;
root /home/www/site.com;
access_log /var/log/nginx/log;
error_log /var/log/nginx/error.log;
index index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
# SECURITY : Deny all attempts to access PHP Files in the uploads directory
location ~* /(?:uploads|files)/.*\.php$ {
deny all;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php-fpm/php.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt {allow all; log_not_found off; access_log off; }
error_page 404 /404.html;
location ~ /\. { deny all; }
location ~* ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off;
log_not_found off;
expires 30d;
}
}
I found in my case the problem was that mod_rewrite was not enabled. I had to run a2enmod rewrite on the server with apache
May be you could get your answer here Ultimate NGINX configuration for Wordpress ยป www.geekytuts.net
After I changed ICG to nginx all routes except index page does not work.
Laravel Config:
#/etc/nginx/sites-enabled/laravel
server {
listen 80;
root /var/www/home;
index index.php;
server_name 192.168.178.71;
access_log /var/www/home/storage/app/logs/laravel-nginx-access.log;
error_log /var/www/home/storage/app/logs/laravel-nginx-error.log error;
location /home {
root /home/public;
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }
# ERROR
error_page 404 /index.php;
# DENY HTACCESS
location ~ /\.ht {
deny all;
}
}
Default config:
# /etc/nginx/sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm;
server_name 192.168.178.71 localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ index.php?$query_string;
autoindex on;
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
}
location ~ \.php$ {
#try_files $uri /index.php =404;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME document_root$fastcgi_script_name;
}
location ~ /\.ht {
deny all;
}
}
my nginx config
#/etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
disable_symlinks off;
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# SSL Settings
##
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
What I tried:
/var/www/home# (home folder is laravel folder)
sudo chown -R www-data:www-data *
/var/www/home#
sudo chown -R root:root *
also I tried to change
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ /index.php$is_args$args;
try_files $uri $uri/ /index.php;
php artisan cache:clear
Mostly questions in google i have read, but nothing helps me.
My phpinfo - link
This is the correct basic config for Laravel and Nginx:
server {
listen 80 default_server;
root /var/www/laravel/public/;
index index.php index.html index.htm;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
# pass the PHP scripts to FastCGI server listening on /var/run/php5-fpm.sock
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
EDIT:
Instead of:
fastcgi_pass unix:/var/run/php5-fpm.sock;
As of November 2018, as PHP 7.2 is out, it would be:
fastcgi_pass unix:/var/run/php7.2-fpm.sock;
When I sent parameters by get I did not recognize them, I just have to activate the following:
try_files $uri $uri/ /index.php$is_args$args;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I had same problem after updating few lines nginx working fine..
It's for windows ( change root acording your file system )
1.root html/laravel; #Update Here - add project folder name after html
2.try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
server {
listen 80; # IPv4
server_name localhost;
## Parametrization using hostname of access and log filenames.
access_log logs/localhost_access.log;
error_log logs/localhost_error.log;
## Root and index files.
root html/laravel; #Update Here - add project folder name after html
index index.php index.html index.htm;
## If no favicon exists return a 204 (no content error).
location = /favicon.ico {
try_files $uri =204;
log_not_found off;
access_log off;
}
## Don't log robots.txt requests.
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
## Try the requested URI as files before handling it to PHP.
location / {
try_files $uri $uri/ /index.php$is_args$args; #Update Here - Add this for 404 not found error
## Regular PHP processing.
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass php_processes;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
## Static files
location ~* \.(?:css|gif|htc|ico|js|jpe?g|png|swf)$ {
expires max;
log_not_found off;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
## Set the OS file cache.
open_file_cache max=1000 inactive=120s;
open_file_cache_valid 45s;
open_file_cache_min_uses 2;
open_file_cache_errors off;
}
## Keep a tab on the 'big' static files.
location ~* ^.+\.(?:ogg|pdf|pptx?)$ {
expires 30d;
## No need to bleed constant updates. Send the all shebang in one
## fell swoop.
tcp_nodelay off;
}
} # / location
}
I had the same issue, but updating the default configuration made it work.
location #rewrite {
rewrite ^/(.*)$ /index.php?_url=/$1;
}
location / {
try_files $uri $uri/ #rewrite;
}
Let me know if this worked for you or not.
sudo service nginx restart after changing the configuration.
Try it, work for me.
sudo nano /etc/nginx/sites-enabled/default
and then sudo systemctl reload nginx
server {
listen 80;
server_name _ midominioexample.com www.midominioexample.com;
root /var/www/html/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I was also getting the same error of Routes not working on Nginx on my Ubuntu 16.04
To solve Routes problem, i tried the following code and its just working fine for me.
Open project conf file using following command
sudo nano /etc/nginx/sites-available/projectname
Then do the following changes in this file
server {
listen 80;
listen [::]:80;
root /var/www/project_name/public;
server_name server_name;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
}
Important thing is to change the try_files in location block.
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
I found that this solved my problem with Laravel routing.
I nested the location ~ .php$ inside location /.
Example:
server{
listen 9000;
server_name _;
root /var/www/myapp/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
}
}
As of 03/2022 and the current version of Laragon (5.0.0) I spent a lot of time to figure out why I can't open any link except index.php that I have configured in web.php for the route:list.
Because I just wanted to turn off SSL and turn it back on. It seems this causes to reset all your config files.
It seems that Laragon by default adds those lines:
# Access Restrictions
allow 127.0.0.1;
deny all;
I have put a # in front of deny to uncomment it and it worked again, like this:
# Access Restrictions
allow 127.0.0.1;
#deny all;
I am successfully running a Wordpress installation alongside Meteor using NGINX. I have no real experience with Wordpress or php, so this may be a simple fix.
The following configuration works:
server_tokens off;
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
upstream mydomain-production {
server localhost:8000;
}
# redirect all non-subdomain traffic to https
server {
listen 80;
server_name www.mydomain.com mydomain.com;
rewrite ^ https://$host$request_uri? permanent;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
}
# this non-subdomain serves meteor correctly
server {
listen 443 ssl spdy;
server_name www.mydomain.com mydomain.com;
access_log /var/log/nginx/mydomain.secure.access.log;
error_log /var/log/nginx/mydomain.secure.error.log debug;
ssl_certificate #...removed...# ;
ssl_certificate_key #...removed...# ;
ssl_stapling on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 5m;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers #...removed...# ;
add_header Strict-Transport-Security "max-age=31536000;";
################################
# additional code will go here #
################################
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://mydomain-production;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
if ($uri != '/') {
expires 30d;
}
}
}
# this temporary subdomain serves wordpress correctly
server {
listen 80;
server_name wordpress.mydomain.com;
access_log /var/log/nginx/mydomain.access.log;
error_log /var/log/nginx/mydomain.error.log;
index index.php index.html index.htm;
root /var/www;
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/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
So since I have Wordpress functioning on a temporary subdomain, I want to make it work on the same domain as Meteor and include location directives to route certain requests to Wordpress instead.
I tried adding the following to the 443 server name:
# additional code
location /blog {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location /wp-admin {
root /var/www;
try_files $uri $uri/ /index.php?q=$uri&$args;
index index.php index.html index.htm;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
include fastcgi_params;
}
After doing this, I get an NGINX 404 page at mydomain/blog. So the location directive is successfully sending the request to /var/www instead of Meteor, but for some reason it is not getting to the Wordpress router. I have linked my NGINX error debug output here.
This was somehow solved by simply moving the root /var/www; and index index.php index.html index.htm; outside of the location directive(s). I would be interested to know why this is necessary if anyone can shed any light on this.