Wordpress not loading on shared NGINX server name - php

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.

Related

Getting '404 Not Found Nginx' while trying to visit Wordpress post

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";

Nginx - Cant fin index.php if not under / (root) location

I have trouble to run davical (php) web calendar. There is no errol log in nginx error logs. When is calendar under \ location everything work. But when i have calendar under /calendar location. it returns 404.
default server root is: /usr/share/nginx/html/default
calendar index.php path: /usr/share/nginx/html/calendar/davical/htdocs\index.php
os: Centos 7
server {
listen 80 default_server;
server_name my_domain_name;
return 301 https://$server_name$request_uri;
}
Https
server {
listen 443 ssl http2;
server_name my_domain_name;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
ssl on;
ssl_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
ssl_certificate_key "/etc/pki/tls/certs/nginx/privatekey.pem";
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
ssl_session_tickets off;
ssl_dhparam "/etc/pki/tls/certs/nginx/dhparam.pem";
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;
resolver 8.8.8.8 8.8.4.4;
ssl_stapling on;
ssl_stapling_verify on;
ssl_trusted_certificate "/etc/pki/tls/certs/nginx/certificate.pem";
add_header Strict-Transport-Security "max-age=31536000;includeSubdomains; preload";
root /usr/share/nginx/html/default;
index index.php index.html index.htm;
include /etc/nginx/default.d/php-fpm.conf;
location /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
php-fpm.conf
location ~ \.php$ {
try_files $uri =404;
fastcgi_param HTTPS on;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
Your existing location ~ \.php$ block serves the /usr/share/nginx/html/default root. You need a nested location to process PHP files under the /calendar URI.
Assuming that your calendar app is designed to work within a subfolder, this may work for you:
location ^~ /calendar {
alias /usr/share/nginx/html/calendar/davical/htdocs;
index index.php;
if (!-e $request_filename) {
rewrite ^ /calendar/index.php last;
}
location ~ \.php$ {
if (!-f $request_filename) { return 404; }
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $request_filename;
}
}
Use the ^~ modifier to prevent the other location ~ \.php$ block from taking precedence (see this document for more). Use $request_filename, as it works with alias. Avoid using try_files with alias (see this issue).

How to run django and wordpress on NGINX server using same domain?

I have tried many ways but do not know how to run Django on example.com and wordpress on example.com/blog
The following running project directory structure for Django and Wordpress.
Django app dir- /home/ubuntu/django
Django app running successfully on - example.com:8000
Wordpress dir - /var/www/html/blog
Wordpress running successfully on - example.com
Nginx configuration
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /var/www/html/blog;
index index.php index.html index.htm;
server_name example.com;
location / {
# try_files $uri $uri/ =404;
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;
include fastcgi_params;
}
}
Note- Django app running by gunicorn, I know the subdomain may be the solution but I do not want that.
How to write nginx configuration for both Wordpress and Django to run Django app on example.com and Wordpress on example.com/blog ?
Thanks alex for helping me out to solve this problem.
Here is the solution
Django app dir- /home/ubuntu/django
Wordpress dir - /var/www/html/blog
NGINX Conf file
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect off;
proxy_set_header Host $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 ~ /blog/.*\.php$ {
root /var/www/html;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /blog {
root /var/www/html;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
try_files $uri $uri/ /blog/index.php;
}
location /static/ {
alias /home/ubuntu/django/your_app_name/static/static_root/;
}
location /media/ {
alias /home/ubuntu/django/your_app_name/media/ ;
}
}
Note- please replace your home and siteurl with http://example.com/blog in wp-location table of wordpress
Now Your Django app running on example.com
Now Your Blog running on example.com/blog
As i understand you have a server running your django site and one running your wordpress site. if so you can do something like this:
{
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name example.com;
access_log /var/log/nginx/example-access.log;
location / {
proxy_pass http://127.0.0.1:3001;
proxy_redirect off;
proxy_set_header Host $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 /blog/ {
proxy_pass http://127.0.0.1:(wordpress port);
proxy_redirect off;
proxy_set_header Host $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;
}
}
if you need to server the php blog from ngix no apache in the middle use somethon like:
location ~ /blog/.*\.php$ {
root /var/www/html/blog;
index index.php index.html index.htm;
set $php_root /var/www/html/blog;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $php_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
Just add these lines to your config file.
location ~ /blog/.*\.php$ {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location /blog {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
try_files $uri $uri/ /blog/index.php;
}

How do make clean urls in nginx?

I'm trying to make the urls for nginx (Ubuntu 14.04) clean, instead of example.com/profile.php it would be example.com/profile, or example.com/about.html to example.com/about
I have tried pretty much all the other ones on stackoverflow however they do not work, including making the site have a 500 internal server error, to making me download the PHP file
Current config
server {
listen 443 ssl;
root /usr/share/nginx/html;
index index.php index.html index.htm;
include /etc/nginx/mime.types;
server_name mydomain.com;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';
location / {
try_files $uri.html $uri $uri/ =404;
}
location ~ \.php$ {
try_files $uri =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;
}
}
server {
listen 80;
server_name mydomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name api.mydomain.com;
location / {
proxy_pass https://mydomain:1337;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 150;
proxy_send_timeout 100;
proxy_read_timeout 100;
proxy_buffers 4 32k;
client_max_body_size 8m;
client_body_buffer_size 128k;
}
}
server {
listen 1337 ssl;
server_name api.mydomain.com;
root /usr/share/nginx/api;
ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
}
Use the try_files [1] directive.
location / {
try_files $uri.php $uri.html =404;
}
In this example, if the browser was requesting /folder/file, it would first try to find file.php and then file.html before giving up and throwing a 404 error.
If that doesn't work, you can use an if statement instead:
location / {
if (-f $request_filename.php) {
rewrite ^ $uri.php;
break;
}
if (-f $request_filename.html) {
rewrite ^ $uri.html;
break;
}
}
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

NGINX: node.js + php on domain + subdomain

I'm trying to set node.js app on main domain and php-based forum on subdomain. Node.JS app works on 8000 port. Here's my config:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
server_name myawesomeapp.ru;
location / {
proxy_pass http://127.0.0.1:8000;
access_log off;
}
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
root /srv/myawesomeapp/static;
}
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.php index.html index.htm;
server_name forum.myawesomeapp.ru
location / {
try_files $uri $uri/ /index.html;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
#fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Both node.js app & php forum can't be reached at myawesomeapp.ru. 127.0.0.1:8000 shows nodejs-app. What's wrong with my config? Thanks.
p.s. my php files are placed in /usr/share/nginx/html
Please include any messages you see on response of trying to visit both vhosts. As well make sure you include this setup in your nginx config as well as service nginx reload after changing configurations.
In order to proxy nginx to node you have to use upstreams. Here is configuration that might suit your needs:
upstream node {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name myawesomeapp.ru;
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
root /srv/myawesomeapp/static
try_files $uri $uri/ =404;
expires 365d;
}
location / {
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://node/;
proxy_redirect off;
}
}
For your forum try this config:
server {
server_name www.forum.myawesomeapp.ru;
rewrite ^(.*) http://forum.myawesomeapp.ru$1 permanent;
}
server {
listen 80 default_server;
server_name forum.myawesomeapp.ru;
root /usr/share/nginx/html;
index index.php;
charset utf-8;
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|pdf|txt|tar|wav|bmp|rtf|js|flv|swf|html|htm)$ {
access_log off;
try_files $uri $uri/ =404;
expires 365d;
}
error_page 404 /404.html;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_intercept_errors on;
}
}
Try just remove this lines from your config:
listen [::]:80 default_server ipv6only=on;

Categories