how to configure ngnix.config for wordpress ?[Permalink error] Using VestaCP - Ngnix php FPM

February 2, 2017 940 views
Development Deployment WordPress LEMP Server Optimization Ubuntu 16.04

Hello. i am facing problem configuring ngnix.config block to support wordpress.

my wordpress shows 404 error when i put is to postname.
others except plain text doesnt seems to be working.

after going through some doc , i figured that ngnix.config need modification to support .htaccess like behavior.

please can anyone check whats the problem?

server block is configured as below
server {
listen 139.59.10.214:80;
servername chetantest.tk www.chetantest.tk;
root /home/admin/web/chetantest.tk/public
html;
index index.php index.html index.htm;
accesslog /var/log/nginx/domains/chetantest.tk.log combined;
access
log /var/log/nginx/domains/chetantest.tk.bytes bytes;
error_log /var/log/nginx/domains/chetantest.tk.error.log error;

location / {

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

    location ~ [^/]\.php(/|$) {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if (!-f $document_root$fastcgi_script_name) {
            return  404;
        }

        fastcgi_pass    127.0.0.1:9003;
        fastcgi_index   index.php;
        include         /etc/nginx/fastcgi_params;
    }

    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;

    if ($scheme = http) {
            return 301 https://$server_name$request_uri;
    }

}

error_page  403 /error/404.html;
error_page  404 /error/404.html;
error_page  500 502 503 504 /error/50x.html;

location /error/ {
    alias   /home/admin/web/chetantest.tk/document_errors/;
}

location ~* "/\.(htaccess|htpasswd)$" {
    deny    all;
    return  404;
}

location /vstats/ {
    alias   /home/admin/web/chetantest.tk/stats/;
    include /home/admin/web/chetantest.tk/stats/auth.conf*;
}

include     /etc/nginx/conf.d/phpmyadmin.inc*;
include     /etc/nginx/conf.d/phppgadmin.inc*;
include     /etc/nginx/conf.d/webmail.inc*;

include     /home/admin/conf/web/nginx.chetantest.tk.conf*;

}

1 Answer

@inboundRaj

Ideally, you don't want to nest location blocks, and from looking over your configuration, that's where the issue is stemming from. Each location block should ideally only handle one thing. So if we take your current server block and rewrite it without nesting and break it apart, it looks like:

server {
    listen 139.59.10.214:80;
    servername chetantest.tk www.chetantest.tk;

    root /home/admin/web/chetantest.tk/publichtml;

    index index.php index.html index.htm;

    accesslog /var/log/nginx/domains/chetantest.tk.log combined;
    accesslog /var/log/nginx/domains/chetantest.tk.bytes bytes;
    error_log /var/log/nginx/domains/chetantest.tk.error.log error;

    error_page  403 /error/404.html;
    error_page  404 /error/404.html;
    error_page  500 502 503 504 /error/50x.html;

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

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

    location ~ [^/]\.php(/|$) {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        if (!-f $document_root$fastcgi_script_name) {
            return  404;
        }

        fastcgi_pass    127.0.0.1:9003;
        fastcgi_index   index.php;
        include         /etc/nginx/fastcgi_params;
    }

    location /error/ {
        alias   /home/admin/web/chetantest.tk/document_errors/;
    }

    location ~* "/\.(htaccess|htpasswd)$" {
        deny    all;
        return  404;
    }

    location /vstats/ {
        alias   /home/admin/web/chetantest.tk/stats/;
        include /home/admin/web/chetantest.tk/stats/auth.conf*;
    }

    include     /etc/nginx/conf.d/phpmyadmin.inc*;
    include     /etc/nginx/conf.d/phppgadmin.inc*;
    include     /etc/nginx/conf.d/webmail.inc*;

    include     /home/admin/conf/web/nginx.chetantest.tk.conf*;
}
Have another answer? Share your knowledge.