A
A
Andrey2017-01-23 18:20:14
Nginx
Andrey, 2017-01-23 18:20:14

How to set up permalinks in Wordpress on nginx?

Here is nginx.conf

server {
    listen      207.154.201.92:80;
    server_name interyer.su www.interyer.su;
    return 301 https://$server_name$request_uri;
    root        /home/admin/web/interyer.su/public_html;
    index       index.php index.html index.htm;
    access_log  /var/log/nginx/domains/interyer.su.log combined;
    access_log  /var/log/nginx/domains/interyer.su.bytes bytes;
    error_log   /var/log/nginx/domains/interyer.su.error.log error;


    location / {

        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:9001;
            fastcgi_index   index.php;
            include         /etc/nginx/fastcgi_params;
        }
    }

    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/interyer.su/document_errors/;
    }

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

    location /vstats/ {
        alias   /home/admin/web/interyer.su/stats/;
        include /home/admin/web/interyer.su/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.interyer.su.conf*;

Why do posts and pages start showing a 404 error when switching permalinks in WordPress from https://interyer.su/?p=123 to https://interyer.su/sample-post/ ?
Option
location / {
                try_files $uri $uri/ /index.php?$args;
        }

taken from here does not work.
Help me set it up.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
I
Igor Vorotnev, 2017-01-23
@HeadOnFire

You don't have a config, but some kind of vinaigrette...
Within listen :80, do you redirect to https?!

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

This fragment is nested in the same
location / {
    ...
    location / {
        try_files $uri $uri/ /index.php?$args;
    }
    ...
}

What is this nonsense? I don't even want to understand this config.
Here is the basic config that works and is responsible for permalinks, including:
server {
        # Слушаем 80й порт
        listen 80; 
        # Обслуживаем доменное имя, www тут же слушать не надо - будут дубликаты контента, печаль для SEO
        server_name example.com;
        # Корневая директория проекта
        root /var/www/example.com/httpdocs;

        # Индексы
        index index.php index.html;

        # Обработка запросов
        # $uri - существует ли конкретный файл
        # $uri/ - существует ли директория
        # /index.php?$args - если это не запрос на существующий файл или директорию, то перебрасываем на роутер WordPress (это и есть то, что надо для пермалинков)
        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        # Обрабатываем PHP
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
                fastcgi_pass unix:/var/run/php5-fpm.sock; # или php7.0-fpm.sock
                fastcgi_index index.php;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                include fastcgi_params;
        }

        # Все остальное

        # Запрещаем доступ к .htaccess
        location ~ /\.ht {
                deny all;
        }

        # Просим кешировать статику на Х дней, не писать в логи
        location ~*
        ^.+\.(js|css|swf|xml|txt|ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
                access_log off;
                log_not_found off;
                expires 30d;
        }

}

# Отдельно слушаем домен с www и редиректим на основной
server {
        # Слушаем 80й порт
        listen 80; 
        # Обслуживаем доменное имя c www
        server_name www.example.com;
        # Отправляем запрос на основной домен
        return 301 $scheme://example.com$request_uri;
}

Commented for clarity.
As for the HTTPS protocol, firstly, it must be listened to on a separate port, and secondly, an SSL certificate must also be connected there.

W
WP_Panda, 2017-01-23
@wp_panda

to Igor's config @HeadOnFire
For ubuntu and 7 php, the default path will be like this
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;

D
dimax77, 2019-03-24
@dimax77

Igor, I made the config as you described, I ran into an incomprehensible problem. I have a VPS on Ubuntu, an nginx server, I installed Wordpress and WooCommerce on one site, links by page and post names work, but when I click on the trash button, some file starts loading, if I set permalinks to an arbitrary view, the button starts working , how to make it work with links by the name of the records? site beton716.ru

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question