The world wide web is full of tutorials on how to setup WordPress and important for our use case also how to do this with nginx. However it seems to me there are only three cases covered: throw WordPress to your document root, access it at / and have a single site installation with nothing else around. The other two cases deal with multisite installations.
This covered not what I wanted. For a recent project we already had an URL like http://foo.example.com/
with running http://foo.example.com/redmine
and http://foo.example.com/phpmyadmin
so I thought if further web applications come along like a wiki or whatever else, it would be a bad idea to put the WordPress to just http://foo.example.com/
and let the aforementioned look like parts of the blog, maybe even interfere with permalinks or making other later decisions difficult. No, I wanted to have the CMS sitting in http://foo.example.com/site/
and http://foo.example.com/
should just contain a static page or redirect with 302 or anything like that.
Turned out configuration was harder than I thought, but that was probably due to my lack of knowledge on how nginx, php5-fpm and an application play together. The config I came up with is this:
server { listen 80; listen [::]:80; server_name foo.example.com; root /usr/share/nginx/www; location ~ ^/$ { return 302 http://$host/site/; } location = /favicon.ico { log_not_found off; access_log off; } location = /robots.txt { log_not_found off; access_log off; } # Redmine Config by someone else client_max_body_size 100M; location /redmine { return 301 https://$host$request_uri; } # WordPress Config by me location /site { alias /srv/wordpress; index index.php; # try_files and alias does not work, see http://trac.nginx.org/nginx/ticket/97 # instead we rewrite by ourselves almost like # https://stackoverflow.com/questions/17805576/nginx-rewrite-in-subfolder # try_files $uri $uri/ /site/index.php?$args; try_files $uri $uri/ @site_rewrite; } location @site_rewrite { rewrite ^/site/(.*)$ /site/index.php?$1; } location ~* /site/(?:uploads|files)/.*\.php$ { deny all; } location ~ ^/site/(.+\.php)$ { alias /srv/wordpress/$1; fastcgi_split_path_info ^(.+\.php)(/.*)$; fastcgi_intercept_errors on; fastcgi_pass php; fastcgi_index index.php; include fastcgi_params; } # phpsysinfo location /phpsysinfo { alias /usr/share/phpsysinfo; index index.php; } location ~ ^/phpsysinfo/(.+\.php)$ { alias /usr/share/phpsysinfo/$1; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass php; fastcgi_index index.php; include fastcgi_params; } }
The trick is to get the alias
and fastcgi_split_path_info
statements right. What really helps here is reading the nginx documentation instead of copying just another config from some blog and trying until something succeeds. So RTFM guys!
Update: until #97 is fixed in nginx, this won’t work with pretty permalinks. :-/
Update: now rewrite like above works with #97 still not fixed.
Nice config. I applied this on a WordPress subfolder installation (removing the sections that I didn’t need) and it worked fine.
Thanks!
Hey!
Thanks for your response on my tweet. Here is my config:
# Blog
location /blog {
alias /home/forge/blog;
index index.php;
try_files $uri $uri/ @site_rewrite;
}
location @site_rewrite {
rewrite ^/blog/(.*)$ /blog/index.php?$1;
}
location ~* /blog/(?:uploads|files)/.*\.php$ {
deny all;
}
location ~\.php$ {
alias /home/forge/blog/$1;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_intercept_errors on;
fastcgi_param SCRIPT_FILENAME $document_root;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
It seems that the correct file is loading but i get a 403 Access denied. I already checkt my files and folders permissions. He tries to access /home/forge/blog but everything should be fine:
find /home/forge/blog -type d -exec chmod 755 {} \;
find /home/forge/blog -type f -exec chmod 644 {} \;
Nevertheless i get:
2015/08/30 09:45:45 [error] 16602#0: *1883 FastCGI sent in stderr: „Access to the script ‚/home/forge/blog/‘ has been denied (see security.limit_extensions)“ while reading response header from upstream, client: 88.65.39.153, server: test.stomt.com, request: „GET /blog/ HTTP/1.1“, upstream: „fastcgi://unix:/var/run/php5-fpm.sock:“, host: „test.stomt.com“
I also added „security.limit_extensions = .php .html“ to my php5-fpm.conf.
I also tried to add the following parameters to the config:
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Thanks in advance!
[…] http://www.netz39.de/2014/installation-of-wordpress-in-a-subdirectory-with-nginx/ […]
Great solutions, i have searched for it for almost 2 days but this solution to setup wordpress on nginx sub directories solved by error of redirecting issue.
I have created extra file in root directory and just defined below lines and added(included) this file in /etc/nginx/sites-available/mysite.conf
# Blog
location /blog {
alias /home/mcs/live/blog;
index index.php;
try_files $uri $uri/ @site_rewrite;
}
location @site_rewrite {
rewrite ^/blog/(.*)$ /blog/index.php?$1;
}
location ~* /blog/(?:uploads|files)/.*\.php$ {
deny all;
}
Thanks guys for helping me out….
Thanks for this. Got permalinks working correctly for me.