W
W
WeReng2016-04-18 14:49:12
Nginx
WeReng, 2016-04-18 14:49:12

How to put Wordpress in a subdirectory (NGINX)?

Hello! Please help, I can't place WordPress in a subdirectory.
The task seems to be quite simple. The site should open at site.ru/blog The
root directory on the server is /home/server/htdocs/site.ru/www/
Now the site is in the blog subdirectory, i.e. /home/server/htdocs/site.ru/www/blog. In the future, it is planned to place several WPs in subdirectories, for example, blog1, blog2, etc. I would like, if possible, that the config does not have to be edited when creating a new blog, and when creating a folder, he understands everything in the WordPress subfolder.
I poke around with the configs and nothing comes out, I read the guides, I try and there is no result. The main site.ru/blog page does not open, but offers to download index.php. At the same time, all other pages work fine, for example site.ru/blog/category/post - will be displayed normally, and site.ru/blog - will offer to download index.php Config
example:

server {
  listen   80;
  server_name site.ru www.site.ru;

  root /home/server/htdocs/site.ru/www/;

  access_log /home/server/logs/site.ru/nginx.access.log;
  error_log /home/server/logs/site.ru/nginx.error.log;
  index index.php index.html index.htm;


  #One request per second
  location = /wp-login.php {
    limit_req zone=one burst=1 nodelay;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    limit_req_status 444;
  }

  #cache static files
   location ~ ^/(images|javascript|js|css|flash|media|static)/ {
     		root /home/server/htdocs/site.ru/www;
      		expires 10d;
            access_log off;
    }

  #Block HotLink
  location ~ \.(jpeg|png|gif|jpg)$ {
    valid_referers none blocked site.ru *.site.ru;
    if ($invalid_referer) {
    return 403;
    }
  }

  error_page 404 /404.html;
  error_page 500 502 503 504 /50x.html;
  location = /50x.html {
    root /etc/nginx/error;
  }

  #Nginx Helper

  set $skip_cache 0;

  # POST requests and urls with a query string should always go to PHP
  if ($request_method = POST) {
    set $skip_cache 1;
  }
  if ($query_string != "") {
    set $skip_cache 1;
  }

  # Don't cache uris containing the following segments
  if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
    set $skip_cache 1;
  }

  # Don't use the cache for logged in users or recent commenters
  if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
    set $skip_cache 1;
  }

  if (!-e $request_filename) {
           rewrite /wp-admin$ $scheme://$host$uri/ permanent;
           rewrite ^(/[^/]+)?(/wp-.*) $2 last;
           rewrite ^/[^/]+(/.*.php)$ $1 last;
   }


  location ~ /purge(/.*) {
      fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
  }



  #WORDPRESS conf
  location / {
  index index.php
    try_files $uri $uri/ /blog/index.php?$args;

  }

  location /blog/ {
    try_files $uri $uri/ /blog/index.php?$args;
  }

  location ~ \.php$ {
    try_files $uri =404;
    include fastcgi_params;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_cache_bypass $skip_cache;
          fastcgi_no_cache $skip_cache;
    fastcgi_index index.php;
    fastcgi_cache WORDPRESS;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
    fastcgi_cache_valid 200 301 202 404 60m;
  }

  location = /favicon.ico {
    log_not_found off;
    access_log off;
  }
  location = /robots.txt {
    allow all;
    log_not_found off;
    access_log off;
  }
  location ~ /\. {
    deny all;
  }
  location ~* /(?:uploads|files)/.*\.php$ {
    deny all;
  }

  rewrite /wp-admin$ $scheme://$host$uri/ permanent;

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
TraiDeR, 2016-04-18
@WeReng

There was a similar problem with downloading index.php. The issue was resolved surprisingly simply - clearing the browser cache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question