aze
aze

Reputation: 850

Cannot get nginx to work with wordpress on a different location

I have the following nginx configuration which works fine :

    #wordpress
    location /wordpress/ {
        index index.php;
        alias /usr/share/webapps/wordpress/;
        try_files $uri $uri/ wordpress/index.php?$args;
    }

    location ~ \.php$ {
        alias /usr/share/webapps/;
        include fastcgi.conf;
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location "^/wordpress/.*\.(js|css|png|jpg|jpeg|gif|ico)$" {
        expires max;
    }

And I am trying to change the url location to "hidden_wordpress", therefore I tried this :

    #wordpress
    location /hidden_wordpress/ {
        index index.php;
        alias /usr/share/webapps/wordpress/;
        try_files $uri $uri/ wordpress/index.php?$args;
    }

    location ~ \.php$ {
        alias /usr/share/webapps/;
        include fastcgi.conf;
        include fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass php;
        fastcgi_buffers 16 16k;
        fastcgi_buffer_size 32k;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location "^/hidden_wordpress/.*\.(js|css|png|jpg|jpeg|gif|ico)$" {
        expires max;
    }

But with this configuration, I keep getting those error logs :

==> nginx_error.log <==
2018/12/18 20:35:38 [error] 30923#30923: *1 FastCGI sent in stderr: "Unable to open primary script: /usr/share/webapps//hidden_wordpress/index.php (No such file or directory)" while reading response header from upstream, client: 127.0.0.1, server: 127.0.0.1, request: "GET /hidden_wordpress/ HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: ""

I didn't move the wordpress contents to the "hidden_wordpress" folder since I don't want to change that.

It seems the url is not properly rewritten for the php part. Any idea of how to achieve this?

Upvotes: 2

Views: 161

Answers (1)

Richard Smith
Richard Smith

Reputation: 49672

Your location ~ \.php$ block cannot correctly resolve URIs that begin with /hidden_wordpress.

Use nested location blocks so that the effect of the alias directive can be inherited.

For example:

location ^~ /hidden_wordpress {
    index index.php;
    alias /usr/share/webapps/wordpress;

    if (!-e $request_filename) { rewrite ^ /hidden_wordpress/index.php last; }

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        ...
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_pass php;
    }

    location ~ \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires max;
    }
}

Use $request_filename to obtain the correct path to the aliased file. Avoid try_files with alias due to this issue. See this caution on the use of if.

Upvotes: 1

Related Questions