Trailing Slash Issue with WP in a Vagrant Dev Environment - wordpress

I have a Wordpress site running in a Vagrant development environment. When I load up http://localhost:8080, the site comes up fine, but as soon as I try to access the admin by going to http://localhost:8080/wp-admin, I'm redirected to http://localhost/wp-admin/.
Two things here:
Something (Wordpress?) is forcing the trailing slash (which would be fine, except...).
In the trailing slash redirection, the port is getting lost (which is very much not fine).
I've tried adding the port_in_redirect directive (using both values, to be honest) that I've seen in other answers to similar questions, but it changed nothing. This seems to be a Wordpress, ahem, feature, but I can't find anything to explain its purpose or that helps me keep it from breaking things. I'm hoping someone can help.
My Nginx server block:
server {
listen 80;
server_name localhost;
root /vagrant/www;
index index.php;
access_log /var/log/nginx/project.vm.access.log;
error_log /var/log/nginx/project.vm.error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests so the admin interface
# works correctly. I've tried with and without this. No difference.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
# Cache static files
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location = /robots.txt {
access_log off;
log_not_found off;
allow all;
}
location ~ /\. {
access_log off;
log_not_found off;
deny all;
}
location ~ ~$ {
access_log off;
log_not_found off;
deny all;
}
}
In my wp-config.php file, I have the WP_HOME and WP_SITEURL constants set.
define('WP_HOME','http://localhost:8080');
define('WP_SITEURL','http://localhost:8080');
Any idea what I'm missing?

I recently face the same problem after moving a WordPress installation from a server to a local Vagrant instance and solved it adding:
define('RELOCATE',true);
to wp-config.php file and going to http://localhost:8080/wp-login.php
I found the solution at http://codex.wordpress.org/Changing_The_Site_URL#Relocate_method, I think there you can find a few other ways to deal with the issue of moving a WP installation from one server to another.

Related

Settings for Wordpress after converting from Apache to Nginx + PHP-FPM

I have a site that is hosted on a server that uses PHP, Apache. I would like to migrate this site another host that is running on PHP-FPM and Nginx. Obviously, changing the nameservers, mysql dump and transferring the actual wp folder is the easier part. But, I would like to know what settings to modify before migrating the site.
As far as I know, the only thing I have to worry about is pretty urls. I may be able to do
try_files $uri $uri/ /index.php?$query_string; if it works. If not however, the I will risk the site being defaces for several hours before reversing the nameserver to it's original state.
There is a great article here: http://codex.wordpress.org/Nginx on NGINX configs for Wordpress, here is the one I use for normal (not-multisite) wordpress. I save this in a separate file (wordpress.conf) and then include it in server blocks for wordpress-powered sites:
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;
}
# Deny access to any files with a .php extension in the uploads directory
location ~* ^/wp-content/uploads/.*.php$ {
deny all;
access_log off;
log_not_found off;
}
# Deny access to any files with a .php extension in the uploads directory for multisite
location ~* /files/(.*).php$ {
deny all;
access_log off;
log_not_found off;
}
# WordPress single blog rules.
# Designed to be included in any server {} block.
# This order might seem weird - this is attempted to match last if rules below fail.
# http://wiki.nginx.org/HttpCoreModule
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
# Directives to send expires headers and turn off 404 error logging.
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires 24h;
log_not_found off;
}
# Uncomment one of the lines below for the appropriate caching plugin (if used).
#include global/wordpress-wp-super-cache.conf;
#include global/wordpress-w3-total-cache.conf;
# Pass all .php files onto a php-fpm/php-fcgi server.
location ~ \.php$ {
# Zero-day exploit defense.
# http://forum.nginx.org/read.php?2,88845,page=3
# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.
# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine. And then cross your fingers that you won'
t get hacked.
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
}

Moodle 2.3 with Nginx vs slash argument rewrite

I'm trying to setup Moodle 2.3 (not 2.5) ver with nginx latest build. There was some advice on this site before. One of them: Moodle 2.0 with Nginx backend.
Apparently as anybody knows, Moodle is using path_info rules to post URL's like this: http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html. To escape all this nightmare, Moodle is offering to disable "Slash arguments" in UI. Which is great. But not for SCORM player which is forcing "Slash argument" despite the previous option. So with disabled "Slash arguments" everything is working and normal. But my only goal is to use SCORM player.
I tried to use the rewrite rule from the link above:
rewrite ^(.*\.php)(/)(.*)$ $1?file=/$3 last;
which is not working in 2.3-2.5 ver. I assume it worked in 1.9.
Now Moodle is using different path:
http://example.com/moodle/pluginfile.php/26/mod_scorm/content/1/index.html
Some of nginx rules:
location ^~ /moodle {
location ~* ^.+\.(?:css|js|htc|xml|jpe?g|gif|png|ico|bmp|svg|swf|pdf|docx?|xlsx?|tiff?|txt|rtf|cgi|bat|pl|dll|aspx?|class|otf|ttf|woff|eot|less)$ {
add_header Access-Control-Allow-Origin *;
access_log off;
expires 30d;
tcp_nodelay off;
try_files $uri =404;
}
location ~* ^/moodle/.*\.php$ {
include includes/fastcgi_params.conf;
try_files $uri #dynamic;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_read_timeout 1200;
fastcgi_keep_conn on;
fastcgi_pass 127.0.0.1:9090;
}
rewrite (^.*\.php)(/.*) $1 last;
}
Please advise how to solve this.
(Answered by the OP in a question edit. Converted to a community wiki answer. See Question with no answers, but issue solved in the comments (or extended in chat) )
The OP wrote:
I solved this by putting rewrite directive in {server} not in {location} section. In my scenario moodle is installed under subfolder: example.com/moodle.
server {
server_name example.com www.example.com;
rewrite ^/moodle/(.*\.php)(/)(.*)$ /moodle/$1?file=/$3 last;
location ^~ /moodle {
try_files $uri $uri/ /index.php?q=$request_uri;
index index.php index.html index.htm;
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9090;
include includes/fastcgi_params.conf;
}
}
}

nginx-apache-wordpress: setup apache/wordpress under subdirectory and behind nginx

I had developed a Rails app and was happy with it, but I was asked to setup Wordpress under subdirectory /wp. After some unhappy hours of trying to make nginx-to-apache proxying work, I gave up copying code from shitty guides and wrote some very short and clear config:
location #wp {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_redirect off;
proxy_pass http://127.0.0.1:8080;
}
location ~ ^/wp(/|$) {
root /home/apache/www;
rewrite ^/wp/?$ / break;
rewrite ^/wp/(.*) /$1 break;
try_files $uri #wp;
}
It solved most of problems I've had experienced, but it won't work for an obvious reason:
Wordpress generates <real_ip>:8080/<url> links and that is not just ugly (I can live with it), but links don't work since Apache listens on localhost only.
How can I tell Apache or Wordpress (or what header should I proxy with Nginx) to make links look like <real_ip>/wp/<url>? Or is my setup faulty by design? I would appreciate any solution or hint, thanks!
Below is my configuration for nginx, which runs a Rails app at the main root, and a WordPress blog under a subdirectory. Perhaps try this?
server {
listen <ip-address>:80;
server_name domain.com;
rewrite ^(.*)$ $scheme://www.domain.com$request_uri? permanent;
break;
}
server {
listen <ip-address>:80;
server_name www.domain.com;
root /www/domain.com/public_html/current/public;
access_log /www/domain.com/logs/access.log;
error_log /www/domain.com/logs/error.log;
location ^~ /blog {
root /www/domain.com/blog/public_html;
index index.php index.html index.htm;
try_files $uri $uri/ /blog/index.php?$args;
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
if ($uri !~ "^/images/") {
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
}
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/domain.com/blog/public_html$fastcgi_script_name;
}
}

Nginx forward www and non-www requests to one directory?

I have a MediaTemple server from which I serve many websites. I use nginx and have the follow config file. I am correctly forwarding all non-www traffic (ie, http://example.com) to the appropriate directory. However, all the www traffic is returning 404 because my config file is looking for /directory-structure/www.sitename.com instead of /directory-structure/sitename.com
How can I have both www and non-www requests go to one directory? Thanks.
server {
listen 80;
server_name _;
root /var/www/vhosts/$host/httpdocs/;
error_page 404 /;
location / {
try_files $uri $uri/ /index.php;
}
location ~ \.php$ {
try_files $uri =404;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
#fastcgi_pass php;
fastcgi_pass 127.0.0.1:9000;
}
location ~* \.(?:ico|css|js|gif|jpe?g|png)$ {
expires max;
add_header Pragma public;
add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}
# this prevents hidden files (beginning with a period) from being served
location ~ /\. { access_log off; log_not_found off; deny all; }
}
Starting with version 0.7.40 Nginx accepts regular expressions in server_name and captures. Thus it's possible to extract a domain name (without www) and use this variable in root directive:
server_name ~^(?:www\.)?(.+)$ ;
root /var/www/vhosts/$1/httpdocs;
Starting with 0.8.25 it is possible to use named captures:
server_name ~^(?:www\.)?(?P<domain>.+)$ ;
root /var/www/vhosts/$domain/httpdocs;
Another syntax to define named captures is (?<domain>.+) (PCRE version 7.0 and later). More on PCRE versions here
Try this and add the following in the above server config:
if ($host = "www.example.com") {
rewrite (.*) http://example.org$1;
}
What happens here, we are instructin nginx to serve the pages as http://example.com even though the browser URL reads http://www.example.com - I hope this works.
UPDATE
Try this for a generic version:
if ($host ~* "www.(.*)") {
rewrite ^ http://$1$request_uri?;
}
Given the potential issues with if as linked to in RakeshS's answer's comments, as well as the fact that RakashS's answer didn't work for me anyway, here's a solution that should be safer and worked for me with Nginx 1.0.14.
Add an additional server entry for each one of your server sections that does a rewrite:
server {
server_name www.yourwebsite.com;
rewrite ^ $scheme://yourwebsite.com$request_uri permanent;
}

nginx rewrite mystery - duplicating hostname and losing https

I am replacing lighttpd with nginx on my development server. I got it working with PHP and SSL, but I'm stumped by what should be a simple rewrite. I need to rewrite URLs from
http[s]://dev.foo.com/signup/123456
to
http[s]://dev.foo.com/signup/index.php?attcode=123456
The rule I am using is:
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
I have tried numerous variations on this, moved it around, put it inside a location block. What happens is the URL is rewritten to:
http://dev.foo.com/dev.foo.com/signup/123456
The hostname is inserted, and it seems to always lose https and go to http.
My nginx.com server section is below. I have read and re-read the nginx docs (as they are) and searched the nginx mailing list, but nothing I've tried has solved this problem.
Ubuntu 8.0.4 LTS in case that matters.
Thanks.
server {
listen 80;
listen 443 default ssl;
server_name dev.foo.com dev.bar.com localhost;
root /var/www/foo;
index index.php index.html;
# ssl cert stuff omitted
charset utf-8;
access_log /var/log/www/dev.access.log main;
location ~ /\. {
deny all;
}
location ~* ^.+\.(inc|tpl|sql|ini|bak|sh|cgi)$ {
deny all;
}
location ~* ^/(scripts|tmp|sql)/ {
deny all;
}
rewrite ^/robots.txt$ /robots_nocrawl.txt break;
rewrite ^/signup/([0-9]+)$ /signup/index.php?attycode=$1 last;
location / {
try_files $uri $uri/ /error_404.php;
}
location ~ \.php$ {
fastcgi_pass localhost:51115;
fastcgi_index index.php;
fastcgi_intercept_errors on;
include fastcgi_params;
fastcgi_param SERVER_NAME $http_host;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
error_page 404 /error_404.php;
}
Don't put HTTP and HTTPS in the same server block. Separate them into two almost-identical server blocks, one for HTTP and one for HTTPS. Otherwise you will confuse all kinds of Nginx internals.

Resources