• We value your experience with Plesk during 2024
    Plesk strives to perform even better in 2025. To help us improve further, please answer a few questions about your experience with Plesk Obsidian 2024.
    Please take this short survey:

    https://pt-research.typeform.com/to/AmZvSXkx
  • The Horde webmail has been deprecated. Its complete removal is scheduled for April 2025. For details and recommended actions, see the Feature and Deprecation Plan.
  • We’re working on enhancing the Monitoring feature in Plesk, and we could really use your expertise! If you’re open to sharing your experiences with server and website monitoring or providing feedback, we’d love to have a one-hour online meeting with you.

Question Nginx - Plesk 12.5 - Wordpress - Site loading incorrect

Flippert

New Pleskian
Hi all,

I have search through the forum but it seems i cant find out how to get Nginx working properly :(.

I really hope someone can send me the right config which i need so i finally got Nginx working :)

Current config
Centos 7 + PHP 7.09
Wordpress is set to use the following permalink settings
/%postname%/ to use friendly names
I need to have friendly names otherwhise because the cache plugin WP-Rocket needs it for smooth running

So i like to setup Plesk using FPM Application served by Nginx instead of Apache.

If i change to FPM served by Nginx not all the content is loaded properly. Some product images arent loading, and some JS and CSS files seems to strugle :(

I add the following on the Additional nginx directives:

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

I also seen a post where i could use the Yoast Sitemap for redirection by adding the following; Cleary that is not working for me.
I like to use the sitemap option aswell.
----------------
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
----------------------

Hopefully someone can help me by creating the right nginx directives. Many thanks in advanced!!
 
This one below works best for me with nginx. Put it in Apache & nginx Settings -- Additional nginx directives

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

add this one, works for me. But my advice will be to use apache in back and nginx in front, as the default config is.
 
Hi bulent,

sorry to say that, but your solution is not a global solution, when you use plugins, which needs additional nginx directives. As you can see in the post:
I also seen a post where i could use the Yoast Sitemap for redirection by adding the following; Cleary that is not working for me.
I like to use the sitemap option aswell.
----------------
rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
cache plugin WP-Rocket
... the thread starter uses additional plugins for his website and your suggestion will not be sufficient without issues, due to the fact that "Yoast SEO" and "WP-Rocket" needs additional nginx rewrites to work as expected.



Hi Flippert,

an example for a "WP-Rocket" nginx - configuration can be found at => "https://github.com/maximejobin/rocket-nginx/blob/master/rocket-nginx.conf":
Code:
###################################################################################################
# Rocket-Nginx
#
# Rocket-Nginx is a NGINX configuration to speedup your WordPress
# website with the cache plugin WP-Rocket (http://wp-rocket.me)
#
# Author: Maxime Jobin
# URL: https://github.com/maximejobin/rocket-nginx
#
# Tested with WP-Rocket version: 2.8.11
# Tested with NGINX: 1.11.3 (mainline)
#
# Version 1.2
#
###################################################################################################

# Add debug information into header
set $rocket_debug 0;

# HTTP Strict Transport Security (to overwrite default)
set $rocket_hsts_value "";

# WP-Content directory (leave as is if you did not alter WordPress' default value)
set $rocket_wpcontent_directory "$document_root/wp-content";

# WP-Content URL (leave as is if you did not alter WordPress' default value)
set $rocket_wpcontent_url "/wp-content";

###################################################################################################
# Do not alter theses values
#
set $rocket_bypass 1;                # Should NGINX bypass WordPress and call cache file directly ?
set $rocket_encryption "";            # Is GZIP accepted by client ?
set $rocket_file "";                # Filename to use
set $rocket_is_bypassed "No";        # Header text added to check if the bypass worked or not. Header: X-Rocket-Nginx-Bypass
set $rocket_reason "";                # Reason why cache file was not used. If cache file is used, what file was used
set $rocket_https_prefix "";        # HTTPS prefix to use when cached files are using HTTPS
set $rocket_hsts 0;                    # Is HSTS is off (0) by default. Will be turned on (1) if request is HTTPS

# HSTS Default value : 1 year, include subdomains.
set $rocket_hsts_value_default "max-age=31536000; includeSubDomains";

###################################################################################################
# PAGE CACHE
#

# Is GZIP accepted by client ?
if ($http_accept_encoding ~ gzip) {
    set $rocket_encryption "_gzip";
}

# Is SSL request ?
if ($https = "on") {
    set $rocket_https_prefix "-https";
    set $rocket_hsts 1;
}

# If HSTS value is not set, use default value
if ($rocket_hsts_value = "") {
    set $rocket_hsts_value "$rocket_hsts_value_default";
}

# If HSTS is disabled, unset HSTS set for Rocket-Nginx configuration
if ($rocket_hsts = "0") {
    set $rocket_hsts_value "";
}

# File/URL to return IF we must bypass WordPress
# index-mobile.html
# index-mobile-https.html
set $rocket_end "/cache/wp-rocket/$http_host/$request_uri/index$rocket_https_prefix.html$rocket_encryption";
set $rocket_url "$rocket_wpcontent_url$rocket_end";
set $rocket_file "$rocket_wpcontent_directory$rocket_end";
set $rocket_mobile_detection "$rocket_wpcontent_directory/cache/wp-rocket/$http_host/$request_uri/.mobile-active";


# Do not bypass if it's a POST request
if ($request_method = POST) {
    set $rocket_bypass 0;
    set $rocket_reason "POST request";
}

# Do not bypass if arguments are found (e.g. ?page=2)
if ($is_args) {
    set $rocket_bypass 0;
    set $rocket_reason "Arguments found";
}

# Do not bypass if the site is in maintenance mode
if (-f "$document_root/.maintenance") {
    set $rocket_bypass 0;
    set $rocket_reason "Maintenance mode";
}

# Do not bypass if one of those cookie if found
# wordpress_logged_in_[hash] : When a user is logged in, this cookie is created (we'd rather let WP-Rocket handle that)
# wp-postpass_[hash] : When a protected post requires a password, this cookie is created.
if ($http_cookie ~* "(wordpress_logged_in_|wp\-postpass_|woocommerce_items_in_cart|woocommerce_cart_hash|wptouch_switch_toogle|comment_author_|comment_author_email_)") {
    set $rocket_bypass 0;
    set $rocket_reason "Cookie";
}

if (-f "$rocket_mobile_detection") {
    set $rocket_bypass 0;
    set $rocket_reason "Specific mobile cache activated";   
}

# Do not bypass if the cached file does not exist
if (!-f "$rocket_file") {
    set $rocket_bypass 0;
    set $rocket_reason "File not cached";
}

# If the bypass token is still on, let's bypass WordPress with the cached URL
if ($rocket_bypass = 1) {
    set $rocket_is_bypassed "Yes";
    set $rocket_reason "$rocket_url";
}

# Clear variables if debug is not needed
if ($rocket_debug = 0) {
    set $rocket_reason "";
    set $rocket_file "";
}

# If the bypass token is still on, rewrite according to the file linked to the request
if ($rocket_bypass = 1) {
    rewrite .* "$rocket_url" last;
}

# Add header to HTML cached files
location ~ /wp-content/cache/wp-rocket/.*html$ {
    add_header Vary "Accept-Encoding, Cookie";
    add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
    add_header X-Rocket-Nginx-Reason $rocket_reason;
    add_header X-Rocket-Nginx-File $rocket_file;
    add_header Strict-Transport-Security "$rocket_hsts_value";
    expires 30d;
    
    #!# HEADER_HTTP #!#
    #!# HEADER_NON_GZIP #!#
}

# Do not gzip cached files that are already gzipped
location ~ /wp-content/cache/wp-rocket/.*_gzip$ {
    gzip off;
    types {}
    default_type text/html;
    add_header Content-Encoding gzip;
    add_header Vary "Accept-Encoding, Cookie";
    add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
    add_header X-Rocket-Nginx-Reason $rocket_reason;
    add_header X-Rocket-Nginx-File $rocket_file;
    add_header Strict-Transport-Security "$rocket_hsts_value";
    expires 30d;
    
    #!# HEADER_HTTP #!#
    #!# HEADER_GZIP #!#
}

# Debug header (when file is not cached)
add_header X-Rocket-Nginx-Bypass $rocket_is_bypassed;
add_header X-Rocket-Nginx-Reason $rocket_reason;
add_header X-Rocket-Nginx-File $rocket_file;

# No HSTS header added here. We suppose it's correctly added in the site configuration


###################################################################################################
# BROWSER CSS CACHE
#
location ~* \.css$ {
    gzip_vary on;
    expires 30d;
    
    #!# HEADER_CSS #!#
}


###################################################################################################
# BROWSER JS CACHE
#
location ~* \.js$ {
    gzip_vary on;
    expires 30d;
    
    #!# HEADER_JS #!#
}


###################################################################################################
# BROWSER MEDIA CACHE
#
location ~* \.(ico|gif|jpe?g|png|svg|eot|otf|woff|woff2|ttf|ogg)$ {
    expires 30d;
    
    #!# HEADER_MEDIAS #!#
}


Pls. keep in mind, that additional plugins for wordpress often need additional configuration(s), especially when you use nginx as well. YOAST.com ist the right place to search for answers, regarding your "Yoast SEO" questions: => "https://kb.yoast.com/kb/configuration-guide-for-yoast-seo/"

An example for a complete "Yoast SEO" nginx - configuration can be found at: => "https://github.com/wphuman/wordpress-nginx-conf/blob/master/sites-available/yoast-wordpress-seo.conf":
Code:
#Yoast sitemap
# https://rtcamp.com/wordpress-nginx/tutorials/plugins/yoast-seo-sitemap/
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
    rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
    rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
    rewrite ^/sitemap_index\.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

    ## following lines are options. Needed for wordpress-seo addons
    rewrite ^/news_sitemap\.xml$ /index.php?sitemap=wpseo_news last;
    rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
    rewrite ^/geo_sitemap\.xml$ /index.php?sitemap=wpseo_local last;
    rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;

    access_log off;
}

If you need further help here, Flippert, pls. note that you should INCLUDE your current wordpress version and ALL plugins in use, because we can't guess your UNIQUE wordpress installation. It is as well a very good idea to include all depending configuration files for investigations and log - entries from your logs ( apache and/or nginx ) help to investigate your issue(s)/problem(s)/error(s) - there is absolutely no "all-in-one" solution, when you use additional plugins for wordpress. ;)
 
Hi

Many thanks for sharing the info! :)

It actually needs alott more to config nginx then i excepcted. I will give it try and see how it goes, i will keep you updated :) THanks!
 
Back
Top