Score:0

Setup wordpress as subpath but in different root

ua flag

Main site: example.com in /var/www/example.com.

Wordpress blog: example.com/blog in /var/www/blog.

Can't make it work.

server {
    listen 80;

    server_name example.com;

    root /var/www/example.com;

    # Add index.php to the list if you are using PHP
    index Index.html index.html index.htm index.nginx-debian.html;

    location ~* \.(ico|jpg|jpeg|png|gif|swf|css|json|txt|sd|html)$ {
            try_files $uri =404;
            access_log off;
            expires 1d;
    }

    location ~* \.(js)$ {
        gzip_static on;
        gunzip on;
        try_files $uri =404;
        access_log off;
        expires 1d;
    }


    location /blog {
        alias  /var/www/blog/;
        #try_files $uri $uri/ /index.php?q=$request_uri;
        try_files $uri $uri/ @blog;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.*\.php)(.*)$;
            try_files $uri =404;
            include       /etc/nginx/fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
            fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_pass unix:/run/php/php7.4-fpm.sock;
            #fastcgi_index index.php;

             #include the fastcgi_param setting
            include fastcgi_params;
        }
    }

    location @blog {
            rewrite ^/blog(.*) /blog/index.php?q=$1;
    }
}

For http://example.com/blog got 403 Forbidden. Looks like alias doesn't work with try_files.

But even http://example.com/blog/index.php doesn't work. Got File not found. .

Paul avatar
cn flag
I think this is common in the WordPress tutorials I've seen: `location try_files $uri $uri/ /blog/index.php?$args;`.
Suvitruf - Andrei Apanasik avatar
ua flag
@Paul it's for case when blog is in subdirectory.
Paul avatar
cn flag
Remove the subdirectory: `location / { try_files $uri $uri/ /index.php?$args; }`
Richard Smith avatar
jp flag
Replace `alias` with `root /var/www;` and use `try_files $uri $uri/ /blog/index.php?$args;`
Suvitruf - Andrei Apanasik avatar
ua flag
@RichardSmith `try_files` doesn't work in this case for `http://example.com/blog`: `directory index of "/var/www/blog/" is forbidden`. But `http://example.com/blog/index.php` works.
Richard Smith avatar
jp flag
You haven't added `index.php` to your `index` directive, so its looking for `index.html` and not finding it. Alternatively, you could place an `index index.php;` statement in the `location /blog` block
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.