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
Related
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";
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 an Ubuntu server with Nginx running node and PHP both.
Requirements:
www.example.com/ -> React App (Running with PM2)
www.example.com/blog -> Running a WordPress Blog
www.example.com/backend -> Running a Symfony app which is the API
Achieved:
React App with PM2
WordPress blog running at /blog with index.php rewrite.
Problem:
Symfony app that is supposed to run on /backend is instead running on /backend/web/app_dev.php and additionally app.php (production mode) throwing a 500 server error. (SCREENSHOTS ATTACHED)
The Nginx configuration is as follows:
# Default Server Settings
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php app.php;
server_name 100.100.100.100;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://localhost:3030/;
proxy_ssl_session_reuse off;
proxy_set_header Host $http_host;
proxy_cache_bypass $http_upgrade;
proxy_redirect off;
}
# WORDPRESS SETTINGS
location #blog {
rewrite ^/blog(.*) /blog/index.php?q=$1;
}
location /blog {
index index.php
alias /var/www/html/blog;
try_files $uri $uri/ #blog;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
access_log /var/log/nginx/blog-access.log;
error_log /var/log/nginx/blog-error.log;
}
# SYMFONY SETTINGS
location #backend {
rewrite ^/backend(.*)$ /web/app.php/$1 last;
}
location /backend {
index app.php
alias /var/www/html/backend/web;
try_files $uri $uri/ #backend;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
access_log /var/log/nginx/symposh-access.log;
error_log /var/log/nginx/symposh-error.log;
}
location ~ /\.ht {
deny all;
}
}
UPDATED SYMFONY SETTINGS (Still not working):
# SYMFONY SETTINGS
location /backend {
index app.php;
alias /var/www/html/backend/web;
try_files $uri $uri/ /backend/web/app.php?$args;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.2-fpm.sock;
}
access_log /var/log/nginx/symposh-access.log;
error_log /var/log/nginx/symposh-error.log;
}
RESULT:
i setted up load balancing with nginx and apache. the nginx is reverse proxy and there is 2 apache web servers in 2 seperate systems.
in my machine, when i request localhost, it works properly. but if i request a file(for example info.php) that is available in 2 other servers, it can't find it and all the times it shows me local info.php file and never shows info.php files on other servers.
if I remove this file (info.php) from my machine, it shows me 404 error.
this is my nginx settings:
upstream localhost {
ip_hash;
server 1.2.3.4;
server 1.2.3.5;
}
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
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_redirect off;
proxy_pass http://localhost;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
the file info.php is available on 2 other servers.
First of all don't use localhost for your upstream name. Why you want to confuse a localhost(current machine) with a upstream localhost. Use below
upstream myservers {
ip_hash;
server 1.2.3.4;
server 1.2.3.5;
}
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name localhost;
location / {
try_files $uri $uri/ =404;
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_redirect off;
proxy_pass http://myservers;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
nginx default config file
server {
listen 80;
server_name domain.com;
root /var/www/html/laravel5-1.0/;
index index.php;
location / {
try_files $uri $uri/ /index.php;
proxy_redirect off;
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
#proxy_cache web_cache;
add_header X-Proxy-Cache $upstream_cache_status;
proxy_pass http://localhost:8081/;
}
# CSS and Javascript
location ~* \.(?:css|js)$ {
expires 1y;
access_log off;
add_header Cache-Control "public";
}
}
Problem: nginx pointing to www directory not the laravel5-1.0 directory application type is laravel
laravel5 is the public directory for my application
nginx and apache running on the same virtual host machine
nginx port 80 apache2 8081
please help me to redirect my page access to laravel5-1.0 directory
try_files does just that, it checks for $uri if it doesn't find that, it will do an internal redirect to /index.php before the proxy_pass.
So if you remove the line
try_files $uri $uri/ /index.php;
It will reach the proxy_pass to http://localhost:8081/
Doc:
http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files