Cauliturtle
Cauliturtle

Reputation: 691

Nginx rewrite in subfolder (404)

I has a site host on a NGINX server which used to work fine to remove index.php in nginx site config using try_files.

But now I am going to add a blog on it, where the URL will be www.foo.com/blog, I can access the blog and use index.php?p=.

But, once I use pretty permalink with Nginx Helper, www.foo.com/blog/2013/07/bar, I get 404.

server {
  # don't forget to tell on which port this server listens
  listen 80;

  # listen on the www host
  server_name foo.com;

  # and redirect to the non-www host (declared below)
  return 301 $scheme://www.ultra-case.com$request_uri;
}

server {
  # listen 80 default_server deferred; # for Linux
  # listen 80 default_server accept_filter=httpready; # for FreeBSD
  listen 80;

  # The host name to respond to
  server_name www.foo.com;

  # Path for static files
  root /web/foo.com

  #index file
  index index.php;

  #Specify a charset
  charset utf-8;

  # Custom 404 page
  error_page 404 /404.html;

  # Uri Rewrite

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

  location / {
    autoindex on;
    # This is cool because no php is touched for static content.
    # include tihe "?$args" part so non-default permalinks doesn't break when using query string
    try_files $uri $uri/ /index.php?$args;
  }
  location ~ \.php$ {
    #NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
    include fastcgi.conf;
    fastcgi_intercept_errors on;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
  }

  # Include the component config parts for h5bp
  include conf/h5bp.conf;
}

Upvotes: 19

Views: 35251

Answers (9)

nphp101
nphp101

Reputation: 1

ip url: 123.123.123/xxxxxxxxxx/

location /xxxxxxxxxx/ {
   try_files $uri $uri/ /xxxxxxxxxx/index.php?$query_string;
}

# Rewrite multisite '.../wp-.*' and '.../*.php'.
if (!-e $request_filename) {
    rewrite ^(/xxxxxxxxxx/.*)+(/wp-.*) /xxxxxxxxxx/$2 last;
    rewrite ^(/xxxxxxxxxx/.*)+.*(/wp-admin/.*\.php)$ /xxxxxxxxxx/$2 last;
    rewrite ^(/xxxxxxxxxx/.*)+(/.*\.php)$ /xxxxxxxxxx/$2 last;

}

Upvotes: 0

ereckers
ereckers

Reputation: 546

The accepted answer routes everything through index.php.
This will break certain script includes, the wp-admin script being one of them.

You can use:

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

Upvotes: 43

paulusm
paulusm

Reputation: 806

I found that with permalink enabled, I needed a combination of both sets of answers given here, otherwise

  1. With only the rewrite, none of the static files got served
  2. With only the try files, the permalinks did not work

This is working on my set up

location /blog/ {
      rewrite ^/blog/(blog/(tag|category|20??)/.*)+$ /blog/index.php?$1;
      try_files $uri $uri/ /blog/index.php?$args =404;
}

Upvotes: 0

amq
amq

Reputation: 505

A universal solution for pretty URLs in root and one subfolder level:

set $virtualdir "";
set $realdir "";

if ($request_uri ~ ^/([^/]*)/.*$ ) {
        set $virtualdir /$1;
}

if (-d "$document_root$virtualdir") {
        set $realdir "${virtualdir}";
}

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

Upvotes: 0

jk2K
jk2K

Reputation: 4557

Try this

location /api {
    # example: http://demo.com/api/channels/dmzb
    root   /data/webserver/demo.com/api/web;
    rewrite ^/api/(.*) /$1 break;
    try_files $uri $uri/ /api/index.php?$args;

    location ~ ^/api/index\.php {
        fastcgi_pass  127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;

        # fix request_uri
        set $changed_request_uri $request_uri;
        if ($changed_request_uri ~ ^/api(.*)) {
            set $changed_request_uri $1;
        }
        fastcgi_param REQUEST_URI $changed_request_uri;

        # fix script_filename
        fastcgi_split_path_info ^(?:\/api\/)(.+\.php)(.*);
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

Upvotes: 1

Jesús Germade
Jesús Germade

Reputation: 9

Think for php, rewrite is no needed with something like this:

location /app/ {
  include fastcgi_params;
  fastcgi_param  SCRIPT_FILENAME /path/to/your/app/index.php;
  fastcgi_pass php;
}

With following fastcgi pass

upstream php {
    server unix:/var/run/php5-fpm.sock;
}

Upvotes: 0

Nicolas Guérinet
Nicolas Guérinet

Reputation: 2236

I would suggest the following, to catch any permalinks under subfolder /blog

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

Upvotes: 5

Mohammad AbuShady
Mohammad AbuShady

Reputation: 42899

Try this, I changed my answer to try to imitate the same behaviour you are using in your rewrite.

location ~ /blog(.*) {
    index index.php;
    try_files $uri /blog/index.php?$1&$args;
}

Upvotes: 1

Cauliturtle
Cauliturtle

Reputation: 691

Um... Thank you for all comments and answer. But finally I use this method to get it works

location /blog {
    index index.php;
    rewrite ^/blog/(.*)+$ /blog/index.php?$1; # it finally works
    # return 200 $request_uri; # it is for inspect what $request_uri is
    # try_files $uri $uri/ /blog/index.php$request_uri$is_args$args; # it gets 500 server error
}

Please point out if current setting has any problems. thank you!

Upvotes: 17

Related Questions