Saturday, February 4, 2017

How to configure Nginx for WordPress permalinks

by Vivek Gite on February 4, 2017 last updated February 4, 2017 I switched from Apache to Nginx web server. How do I configure permalinks under WordPress blog?WordPress has the ability capacity to create a custom URL structure for your blog posts and archives. There are three basic types of WordPress permalinks:
  • Ugly: https://www.cyberciti.biz/faq/?p=2284
  • Pretty using mod_rewrite: https://www.cyberciti.biz/faq/bash-for-loop/
  • Almost pretty using PHP PATHINFO: https://www.cyberciti.biz/faq/index.php/bash-for-loop/
  • In the Settings ? Permalinks panel, you can choose one of the more common permalink structures as follows:

    Fig.01: WordPress permalinks: Choosing your permalink structure

    Fig.01: WordPress permalinks: Choosing your permalink structure

    Edit your nginx.conf file, run:$ sudo vi /etc/nginx/site-enabled/cyberciti.biz.confEdit/add/append the following location block within the server block:

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

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

    If your WordPress blog is in /faq/ sub-directory, try:

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

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

    Save and close the file. Restart/reload your nginx server, run:$ sudo systemctl reload nginxOR$ sudo /usr/sbin/nginx -s reloadHere is my sample config file:

    # Upstream to abstract backend connection(s) for PHP. upstream php { server unix:/run/php/php7.0-fpm.sock; } server { server_name www.cyberciti.biz; root /var/www/html; location /faq/ { try_files $uri $uri/ /faq/index.php?$args; } #Add trailing slash to */wp-admin requests. rewrite /faq/wp-admin$ $scheme://$host$uri/ permanent; # Directives to send expires headers and turn off 404 error logging. location ~* ^.+\.(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 max; } # Pass all .php files onto a php-fpm/php-fcgi server. index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include /etc/nginx/fastcgi_params; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; } }

    # Upstream to abstract backend connection(s) for PHP. upstream php { server unix:/run/php/php7.0-fpm.sock; } server { server_name www.cyberciti.biz; root /var/www/html;location /faq/ { try_files $uri $uri/ /faq/index.php?$args; }#Add trailing slash to */wp-admin requests. rewrite /faq/wp-admin$ $scheme://$host$uri/ permanent;# Directives to send expires headers and turn off 404 error logging. location ~* ^.+\.(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 max; }# Pass all .php files onto a php-fpm/php-fcgi server. index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default) include /etc/nginx/fastcgi_params; fastcgi_index index.php ; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_pass php; } }

    Share this tutorial on:

    Your support makes a big difference:I have a small favor to ask. More people are reading the nixCraft. Many of you block advertising which is your right, and advertising revenues are not sufficient to cover my operating costs. So you can see why I need to ask for your help. The nixCraft, takes a lot of my time and hard work to produce. If you use nixCraft, who likes it, helps me with donations:Become a Supporter →    Make a contribution via Paypal/Bitcoin →   


    Source: How to configure Nginx for WordPress permalinks

    No comments:

    Post a Comment