Wordpress Permalink Issue - extra "index.php" in pretty permalink - wordpress

I migrated my site from Apache to Nginx,the problem now is my permalinks have an extra index.php in the URL, I am not able to determine if this is a wordpress issue or my configuration issue.
Example when my post is rendered the original link: http://hs.com/2012/04/30/a-new-question/
is now being linked as http://hs.com/*index.php/*2012/04/30/a-new-question/
I tried the nginx compatibility plug in.. also tried the custom structure options with and without the plug in and I am just not able to understand what is going on. Wordpress says that the custom permalink structure is saved, even then all links in the site are rendered incorrectly.
Here is my nginx configuration for the site.
server {
server_name harshasagar.com www.harshasagar.com;
access_log /srv/www/harshasagar.com/logs/access.log;
error_log /srv/www/harshasagar.com/logs/error.log;
root /srv/www/harshasagar.com/public_html;
if ($host ~* www\.(.*)) {
set $host_without_www $1;
rewrite ^(.*)$ http://$host_without_www$1 permanent; # $1 contains '/foo', not 'www.mydomain.com/foo'
}
location / {
index index.html index.htm index.php;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /srv/www/harshasagar.com/public_html$fastcgi_script_name;
}

Replace line:
try_files $uri $uri/ /index.php?q=$uri&$args;
with
try_files $uri $uri/ /index.php?$args;
And install this plugin - http://wordpress.org/extend/plugins/nginx-helper/
If you face issue after that please let me know.

Related

Trailing slash/redirection issue NGINX

I am using Drupal 8 with nginx . I have a multiProject Environment with single domain.
I have 10 Drupal sites. which works as
https://example.com/site1
https://example.com/site2
Each site has its on distinct docker container and everything is running smooth on production. But I noticed A issue with few sites. They give 404 without a trailing slash .Only 4 of them. Rest six automatically appends the slash when I remove them and hit in browser.
The nginx config for all are exactly same.
Here is the nginx config for a site :
server
{
client_max_body_size 128m;
root /var/www/html;
index index.php index.html index.htm;
location /site1 {
try_files $uri/ $uri /site1/index.php?$query_string;
}
error_page 404 /404.html;
error_page 403 /403.html;
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
try_files $uri $uri/ /index.php?q=$uri&$args;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
}
I have tried every thing , rewrite, try files and returns.
can any one help.

Wordpress and NGINX /wp-admin redirect loop

My nginx.conf has a server blog containing this:
location ~ \.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/blog;
include /etc/nginx/mime.types;
try_files $uri $uri/ /index.php?q=$uri&$args;
}
But with these settings when I try to access /blog/wp-admin my browser gets stuck in some redirect loop.
If I change the root URLs in nginx.conf to /var/www/html, /blog/wp-admin works, but my post permalinks give me a 404 error.
My WP files are located in /var/www/html/blog. I have 'SSL Insecure Content Fixer' plugin installed because my images giving a mixed content error on my site, which has a Cloudflare page rule to always use SSL.
My WP address and WP home are both set to http://xxx/blog.
Anybody fixed something similar?
Thanks
I think that the main problem is an inconsistency with your root directive. Your PHP configuration has WordPress in /var/www/html/blog whereas your static configuration has WordPress in /var/www/html/blog/blog.
Assuming that WordPress is installed in the root of /var/www/html/blog and that the URIs should be prefixed with /blog/ for both real files and permalinks, the correct URI for the entry point should be /blog/index.php.
The nginx.conf file should probably be:
root /var/www/html;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
location /blog {
include /etc/nginx/mime.types;
try_files $uri $uri/ /blog/index.php;
}
If you have a conflicting root directive within the outer server container, the above root directive could be placed inside the two location blocks unmodified.
I would try /blog/index.php rather than /blog/index.php?q=$uri&$args as the last element of try_files because in my experience, WordPress uses the REQUEST_URI parameter to route permalinks rather than the q argument as you have implied, but YMMV.
If you do have other applications in this servers root and would like to segregate the WordPress root more completely, you might nest the PHP location block like this:
location ^~ /blog {
root /var/www/html;
include /etc/nginx/mime.types;
try_files $uri $uri/ /blog/index.php;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
include fastcgi_params;
}
}

Laravel and WordPress integration routing using nginx

I am developing web app in Laravel 5.2. I have existing WordPress site. So, I want to integrate Laravel with WordPress. WordPress app has static pages. I have two separate directories for Laravel and WordPress in my root directory.
laraApp
wpApp
I want to make wpApp as default app. So when user clicks login button, user will be redirected to laraApp. I want wpApp at www.example.com and laraApp in www.example.com/laraApp. I have nginx web server running. So what should be my nginx config file?
Current nginx config file is :
server {
listen 80;
root /var/www/root/wpApp;
index index.php index.html index.htm;
server_name www.example.com;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# rewrite rules for laravel routes
location /laraApp {
rewrite ^/laraApp/(.*)$ /laraApp/public/index.php?$1 last;
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Here my Laravel app is accessible using url www.example.com/laraApp/public/
I want to access it using www.example.com/laraApp.
Thanks.
The configuration would be simpler if the base URI for each of the applications did not overlap. But given the constraints of your question, you must use two distinct document roots for the PHP section of each of the applications in your configuration.
As you have placed one of your applications in /, the other application is kept separate by the use of nested location blocks. Notice the use of the ^~ modifier to prevent the first PHP block from processing Laravel requests.
index index.php index.html index.htm;
root /var/www/root/wpApp;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ^~ /laraApp {
rewrite ^/laraApp(.*)$ /laraApp/public$1 last;
}
location ^~ /laraApp/public {
root /var/www/root;
try_files $uri $uri/ /laraApp/public/index.php?$query_string;
location ~ \.php$ {
try_files $uri /laraApp/public/index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
}
I am away from my test system at the moment so the above has not been syntax checked or tested.

How to install one site inside another with Nginx

I have a wordpress multisite on centos 6.5 at /var/www/html/site1
accessible at site1.com and everything there is fine.
I would like to install a single site called site2 at
/var/www/html/site2 but
accessible at site1.com/site2
The .conf for site1 has a server block like this:
location / {
try_files $uri $uri/ /index.php?$args;
}
#editing here to...
location /site2 {
root /var/www/html/site2/;
index index.php index.html index.htm;
try_files /index.html /index.php $uri $uri/;
}
location ~ /site2/(.*\.php)$ {
root /var/www/html/site2/;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$1;
include /etc/nginx/fastcgi_params;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
}
#end editing.
When I use try files try_files /index.html /index.php $uri $uri/; it works.
When I use try_files try_files /index.php $uri $uri/; the browser downloads the file instead of displaying it.
The location block should do the trick as long as you define a new root for the location:
location /site2
{
root /var/www/html/site2;
#put your rewrite rules here
}
You have to make sure that your fastcgi (I assume you'r using php-fpm) configuration uses the new docroot as well.
I don't know wordpress very well, but I reckon you also have to configure the second site to be in a subdirectory called /site2, otherwise links might not work properly.

Nginx sites-available doesn't work

I've got this really simple server block under sites-available.
Problem: When I try to access to mydomain.com, Nginx returns a « 404 Not Found », but if I try to access to a file in particular, it works fine, like mydomain.com/index.php
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location / {
try_files $uri =404;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}
Note that:
my hosts file is configured ;
I restart Nginx after each edit ;
the access rights, user and group are correct ;
the error.log file is empty, the access.log returns me all the 404 ;
I tried to change the config by adding/removing some lines, still no changes ;
the site is enabled in sites-enabled with a correct symlink (I tried to edit it and it opened the right file) ;
I've got a few sites on the same server who runs well (so the including of sites-available and sites-enabled is OK, and Nginx works fine).
So, the answer was giver to me on ServerFault by Alexey Ten, here is a copy of the answer
Your try_files directive is too restrictive and, I guess, is in wrong place.
Either remove location / completely, it doesn't makes much sense, or, at least add $uri/ so index directive will work.
try_files $uri $uri/ =404;
But my guess is, you need to move this try_files into location ~ \.php$, this will make sure that php-file exsists before pass it to PHP-FPM for processing. All other files will be served by nginx with proper use of index directive.
server {
listen 80;
index index.php;
server_name mydomain.com;
root /home/myusername/sites/mydomain.com/htdocs;
access_log /home/myusername/sites/mydomain.com/logs/access.log;
error_log /home/myusername/sites/mydomain.com/logs/error.log;
location ~ \.php$ {
try_files $uri =404;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
}

Resources