500 error with Nginx and WordPress pretty permalinks - php

Ran into a 500 issue when running Nginx and WP together and setting pretty permalinks. I've been trying a bunch of different methods from Google but none seems to help.
Config -
server {
listen 80;
root /var/www/mydomain.com/public_html;
index index.php index.html index.htm;
server_name .mydomain.com;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
All files load perfectly well, and the pages work if using the default permalinks setting. Strange thing is, if I check the network log I first see a 200 OK being received, then immediately followed by a 500. Any ideas?
Edit: Setting to close as I'm switching to Apache instead. Will mark correct answer as it seems to have helped others.

try:
try_files $uri $uri/ /index.php?q=$uri&$args;
AND:
fastcgi_index /index.php;
(note the / )

Also above answer by Rufinus is correct.
Here is correct . You Forget to define Index . So nginx don't know which file to lockup and process. Hope this help u out.
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
Your Config does not have fastcgi_parm .
add this line to your config. Its main part of Nginx Config.
change /home/public_html with your website path location
fastcgi_param SCRIPT_FILENAME /home/public_html$fastcgi_script_name;
Here is my Whole wordpress config file. I Hope that works. Its working Nginx config file with pretty urls.
server {
listen 80;
server_name example.com;
root /home/public_html;
index index.php index.htm index.html;
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location /favicon.ico { access_log off; log_not_found off; }
location ~* \.(jpg|jpeg|gif|png|js|xml)$ {
access_log off;
log_not_found off;
expires 360d;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~ .php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /home/public_html$fastcgi_script_name;
include fastcgi_params;
}
}

Related

Ubuntu 18.04 Laravel 5.7 nginx 1.14 Laravel routes not working

I recently moved my server from apache to nginx, when using apache i had a working site using the Laravel framework.
For some reason no pages other than the base index page just return a 404 error. I am sure it has something to do with my nginx config. My currently config is shown below.
server {
listen 80;
server_name munkeemagic.munkeejuice.co.uk;
root /var/www/html/munkeemagic/mtg-webby/mtg/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$is_args$args;
}
location ~* \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I have checked to make sure that all the file permissions are set for user www-data which is the user nginx is using. I have even tried changing
location / {
try_files $uri $uri/ /index.php?$is_args$args;
}
to
location / {
try_files $uri $uri/ index.php?$query_string;
}
but that has not had any impact and i still see the same issue.
So basically with this config if i navigate to http://munkeemagic.munkeejuice.co.uk then the page displays
if i go to http://munkeemagic.munkeejuice.co.uk/login i get a 404 error.
Does anyone have any idea what i may be doing wrong ?
After following user3647971's advice i have modified the config as follows :
server {
listen 80;
server_name munkeemagic.munkeejuice.co.uk;
root /var/www/html/munkeemagic/mtg-webby/mtg/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 index.html;
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.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
I have restarted nginx to take the changes and have also cleared the routes cache, but unfortunately i still have the same problem.
Ok figured out the problem, tbh i was a bit stupid. My default configuration was overriding my website specific config. I unlinked the default config restarted nginx and everything started to work correctly.

Wordpress with index.html on nginx (debian)

I have a server with nginx (php-fpm), but I need the index to be index.html.
My configuration is this:
server {
listen 80 default_server;
#SSL configuration listen *:443 ssl http2;
ssl_certificate /path/to/my.dev.pem;
ssl_certificate_key /path/to/my-key.pem;
root /home/path/to/misite.dev;
index index.php index.html index.htm;
server_name misite.dev ;
location / {
try_files index.html $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.1-fpm.sock;
fastcgi_index index.php;
include fastcgi.conf;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|eot|otf|ttf|woff)$ {
add_header Access-Control-Allow-Origin *;
add_header Cache-Control "public, max-age=31536000, immutable";
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
}
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
}
By having this, only the index.html works, but nothing with php, and by removing the location's index.html, everything works fine with php, but not the index.html
location / {try_files -> index.html <- $ uri / /index.php?$args;}
Do you know how I can do it so that both cases work?
(index.html and dynamic pages with php like mysite.com/tag/my-tag)
I hope I have explained well.
Using /index.html as the first parameter to try_files will break things.
The index directive will look for matching files in the order presented. See this document for details.
So if you have a directory containing more than one type of index file, simply construct your index statement in the order you would like Nginx to prioritise them.
For example:
index index.html index.php;

WordPress wrong redirect while accessing wp-admin

I have a little problem.
Now, I'm migrating my WordPress site from Apache2 to Nginx.
Everything works fine, but not admin panel.
When I try to access '/wp-admin' I have the following redirect:
http://domain.com/wp-login.php?redirect_to=http%3A%2F%2Fdomain.com%2Fwp-admin%2F&reauth=1
After that a blank page appears and nothing else.
I have checked PHP configuration and sample test.php is printing something so it works.
What I'm doing wrong?
Here is my Nginx config:
server
{
listen 155.123.12.222:80;
server_name domain.com www.domain.com ;
access_log /var/log/nginx/domains/domain.com.log;
access_log /var/log/nginx/domains/domain.com.bytes bytes;
error_log /var/log/nginx/domains/domain.com.error.log;
root /home/admin/domains/domain.com/public_html;
index index.php index.html index.htm;
location / {
index index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /wp-admin {
index index.php index.html index.htm;
try_files $uri /wp-admin/index.php?q=$uri&$args;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
location ~ /\. { access_log off; log_not_found off; deny all; }
location ~ \.php$
{
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/nginx_limits.conf;
if (-f $request_filename)
{
fastcgi_pass unix:/usr/local/php56/sockets/admin.sock;
}
}
location ~ /\.ht
{
deny all;
}
include /etc/nginx/webapps.conf;
}
Try removing the following lines:
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location /wp-admin {
index index.php index.html index.htm;
try_files $uri /wp-admin/index.php?q=$uri&$args;
}
Ok, I found a solution.
I have turned on a DEUB mode in wp-config.php. There was a bad configuration in some plugin, so I changed it.
Anyway, thanks for your attention guys.

Nginx server (not showing changes) - Delete cache?

I'm running a Laravel Site (Ubuntu) on Nginx (Not a virtual box). When I make changes to a css file, or any file for that matter i'm unable to see the changes right away. I've tried changing sendfile from on to off as noted in this link:
How to clear the cache of nginx?
And I couldn't find a Nginx cache file to delete the cache. Many sites recommend going to the "path/to/cache" folder but I can't find it:
https://www.nginx.com/blog/nginx-caching-guide/
Anyway I don't believe I set up any kind of proxy caching or anything. Any ideas where I can find my cache so I can delete it folks? For that matter am I looking to do the correct thing here? Thanks for you answer in advance.
This is what my test server block looks like:
server {
# secure website with username and password in htpasswd file
auth_basic "closed website";
auth_basic_user_file /tmp/.htpasswd.txt;
listen (myip) default_server;
listen [::]:83 default_server ipv6only=on;
root /var/www/test/(sitefolder);
index index.php index.html index.htm;
# Make site accessible
server_name (myiphere);
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;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =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;
include fastcgi_params;
}
This is what my real site server block looks like:
server {
# secure website with username and password in htpasswd file
auth_basic "closed website";
auth_basic_user_file /tmp/.htpasswd.txt;
listen (myip) default_server;
listen [::]:81 default_server ipv6only=on;
root /var/www/(mysite)/public;
index index.php index.html index.htm;
# Make site accessible from http://localhost/
server_name (myip);
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;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location ~ \.php$ {
try_files $uri /index.php =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;
include fastcgi_params;
}
First, css is something that the client caches, not the server. You should clear your browser's cache to see your css changes. Furthermore, browser caching is a common problem in css and javascript development. Luckily, you can take advantage of elixir, which ships with laravel 5.1, to prevent browsers caching your assets.
Visit the docs for more info on laravel elixir.
http://laravel.com/docs/5.1/elixir#versioning-and-cache-busting
Second, laravel gives us a convenient command to clear your server's cache:
php artisan cache:clear
UPDATE:
server {
listen 80;
server_name example.com;
root "/usr/share/nginx/app/public";
index index.html index.htm 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; }
access_log off;
error_log /var/log/nginx/app-error.log error;
client_max_body_size 100m;
include hhvm.conf;
location ~ /\.ht {
deny all;
}
}
server {
listen 80;
server_name beta.example.com;
root "/usr/share/nginx/beta/public";
index index.html index.htm 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; }
access_log off;
error_log /var/log/nginx/app-error.log error;
client_max_body_size 100m;
include hhvm.conf;
location ~ /\.ht {
deny all;
}
}

nginx & wordpress - config for permalinks doesn't work; downloads php-files instead

I've installed nginx and I try to run wordpress on it.
Everything works fine, except for permalinks.
Here is the vhost-file I'm using:
server {
listen 123456:80;
server_name my-domain.com;
if ($host ~* www\.(.*)) {
set $wwwless $1;
rewrite ^(.*)$ $scheme://$wwwless$1 permanent;
}
root /var/www/my-folder;
index index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
(I've replaced critical data in the above code with my-domain.com, my-folder and the ip 123456.)
index.php, the admin-panel and using the standard links (.../?p=123) work finde. If I enable some of the permalinks, index.php and the admin-panel still work. But if I try to open another site of the wordpress blog, my browser downloads the index.php :(
Try something like this for the .php location config:
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
Here's my wordpress config modified to suit you.
server { # the redirecting from www to non-www
server_name www.example.com;
return 301 $scheme://example.com$request_uri$is_args$query_string;
}
server {
listen 80;
server_name example.com;
root /path/to/root;
# make sure the directory /var/log/nginx exists
access_log /var/log/nginx/wordpress_access.log;
error_log /var/log/nginx/wordpress_error.log;
index index.php;
location / {
try_files $uri /index.php$request_uri$is_args$query_string;
}
location ~ \.php {
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
location ~ /\.ht { # avoid downloading htaccess, htpasswd, etc files
deny all;
}
}
you can try this vhost file
server {
listen 80;
server_name www.example.com example.com;
root /var/www/www.example.com/web;
if ($http_host != "www.example.com") {
rewrite ^ http://www.example.com$request_uri permanent;
}
index index.php index.html;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}

Categories