Wordpress url rewrite on nginx when wordpress is n

2019-08-03 01:51发布

问题:

For some reason I always have trouble getting Wordpress to work on nginx.

I have my Wordpress installation in the folder "site" (inside public_html). This is my nginx configuration:

server {
    server_name www.wouterds.be wouterds.be;
    access_log /srv/www/www.wouterds.be/logs/access.log;
    error_log /srv/www/www.wouterds.be/logs/error.log;

    location / {
        root /srv/www/www.wouterds.be/public_html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /site/index.php?$args;
        autoindex on;
    }

   location ~ \.php$ {
        include /etc/nginx/fastcgi_params;
        fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /srv/www/www.wouterds.be/public_html$fastcgi_script_name;
    }
}

When I visit my website http://wouterds.be/ I get just the directory index. When I visit http://wouterds.be/blog/ I get the right page and everything is working. But I have no idea how to get it work on the base url.

Anyone who can help me out a bit? I'm certainly not an nginx expert and after 4 hours Google-ing I decided to give it a try here.

回答1:

You can also try (not in a location block):

rewrite /wp-admin$ $scheme://$host$uri/ last;
rewrite ^/(wp-.*.php)$ /wp/$1 last;
rewrite ^/(wp-(content|admin|includes).*) /wp/$1 last;

Exchange /wp/ for /site/ or whatever your wordpress directory is.

Source:

https://wordpress.stackexchange.com/questions/138648/nginx-rules-for-subdomain-multisite-install-bedrock



回答2:

Just change:

    root /srv/www/www.wouterds.be/public_html;

to

    root /srv/www/www.wouterds.be/public_html/site;

assuming wordpress index.php is inside site folder.

Also move root line outside & above location block.

You can try...

server {
  #other stuff 

    root /srv/www/www.wouterds.be/public_html/site ;

    location / {
        root /srv/www/www.wouterds.be/public_html;
        index index.php index.html index.htm;
        try_files $uri $uri/ /site/index.php?$args;
        autoindex on;
    }

  #other stuff 
}