Support » Plugin: WordPress SEO Plugin - Rank Math » Sitemap add rewrite rule nginx

  • Resolved Rob

    (@seomandarin)


    Hi,

    I have exactly this problem as described in the FAQ:
    https://rankmath.com/kb/fix-sitemap-issues/#nginx-server-error

    I’m used to working with Apache so forgive me for my question.
    I tried adding this to /etc/nginx/nginx.conf:

    # START Nginx Rewrites for Rank Math Sitemaps
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    # END Nginx Rewrites for Rank Math Sitemaps

    However it breaks nginx.
    I also tried:

    server   {
    server_name domain.com;
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    }

    No luck.

    What’s the correct format of inserting a rewrite rule?

    Thank you!

Viewing 15 replies - 1 through 15 (of 15 total)
  • Hello @seomandarin

    Please try the following format:

    server {
      rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
      rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    }

    If that doesn’t help, please check the server’s error log right after nginx breaks to see the exact cause of the issue, and let us know what it says, so we can suggest a solution.

    Thank you.

    Rob

    (@seomandarin)

    Hi @pbalazs

    With that format I get error:
    2019/12/30 03:34:41 [alert] 14991#14991: *3173 open socket #18 left in connection 9
    2019/12/30 03:34:41 [alert] 14991#14991: *3174 open socket #19 left in connection 83
    2019/12/30 03:34:41 [alert] 14991#14991: aborting
    2019/12/30 03:34:41 [emerg] 19035#19035: “server” directive is not allowed here in /etc/nginx/nginx.conf:5

    Thank you!

    Hi,
    Could you please share the current contents of the config file? You can use pastebin or similar service if the content is long.

    If the file contains a http {...} block then please try adding the server block inside of that.

    Hope that helps.

    Rob

    (@seomandarin)

    Hi @pbalazs

    I have put it in the http block without any errors. However still get the 404 page.
    https://domain.com/?sitemap=1 does output the sitemap correctly.

    nginx.conf:
    https://pastebin.com/FgGT63px

    Thanks!

    Plugin Author Rank Math

    (@rankmath)

    Hi @seomandarin

    Are you still getting the same “server” directive is not allowed error when the server block is inside the http block?

    Also, please try adding it to the end of the http block, right before its closing } character.

    Thank you.

    Rob

    (@seomandarin)

    Hi @rankmath

    I have now placed it in the bottom of the http block. I don’t get any error. However still get the ‘404 not found page nginx’ page.

    I’ve already tried saving permalinks.

    Any thoughts?

    Thank you for your message

    Regards

    Plugin Author Rank Math

    (@rankmath)

    Hi,

    1. Is your site installed in a sub-directory, by any chance? In other words, is your site URL like example.com or like example.com/wp/?

    2. Does this URL format also show the sitemap correctly? https://example.com/index.php?sitemap=1

    3. Please try the following URL formats and let us know if they show 404s or anything else:

    https://example.com/sitemap.xml
    https://example.com/sitemap_index.xml
    https://example.com/page-sitemap.xml

    Thank you.

    Rob

    (@seomandarin)

    Hi @rankmath

    1) No subdirectory. It is however a WPML multisite installation (which means the installation also has domain.nl and domain.cn. This shouldn’t matter I think since domain.com is still the homepage and everything is done from regular domain.com/wp-admin dashboard

    2) https://domain.com/index.php?sitemap=1 shows correctly.

    3) https://example.com/sitemap.xml https://example.com/sitemap_index.xml https://example.com/page-sitemap.xml
    all get 404

    Thank you

    • This reply was modified 7 months, 1 week ago by Rob.
    • This reply was modified 7 months, 1 week ago by Rob.
    Rob

    (@seomandarin)

    Hi @rankmath

    I’ve replied, but waiting moderation. Here my simplified answer to that

    1. No subdirectory. However i’m using https://wpml.org/tutorials/2016/04/use-wpml-different-domains-per-language
    2. Opens correctly
    3. All get 404

    Thank you!

    • This reply was modified 7 months, 1 week ago by Rob.
    Plugin Author Rank Math

    (@rankmath)

    Hi,

    It sounds like the config file is valid now but the rewrite rules are ignored for some reason, perhaps another rule is taking priority, but I don’t see anything in the config file that would cause this.

    Enabling the rewrite module with the rewrite_log directive might give a clue about what’s happening: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite_log

    Hope that helps.

    Rob

    (@seomandarin)

    Hi @rankmath

    I’ve enabled rewrite_log in http and server. Logging set to notice level.
    The only error I’ve been getting when restarting nginx is

    2019/12/31 02:50:57 [alert] 12385#12385: *63 open socket #36 left in connection 7
    2019/12/31 02:50:57 [alert] 12385#12385: *62 open socket #19 left in connection 18
    2019/12/31 02:50:57 [alert] 12385#12385: aborting

    *scratching head*

    Thanks for thinking with me though

    Plugin Author Rank Math

    (@rankmath)

    Hi @seomandarin

    Please make sure to reload nginx after changing the config file. Once rewrite_log is enabled, whenever a rewrite takes place you should see something like this in the log:

    [notice] 14385#14385: *1 “^(/data/.*)/x/(\w+)\.?.*$” matches “/data/x/test”, client: 192.168.101.1, server: localhost, request: “GET /data/x/test HTTP/1.1”, host: “192.168.101.10”
    [notice] 14385#14385: *1 rewritten data: “/data/y/test.html”, args: “”, client: 192.168.101.1, server: localhost, request: “GET /data/x/test HTTP/1.1”, host: “192.168.101.10”

    You may also try wrapping the rewrite rules in a location block:

    server {
      location / {
        rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
        rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
      }
    }

    Another thing you can try is setting the logging to debug level, which should give detailed information of what nginx does while processing each request and response.

    You can find some other tips and strategies for debugging the rewrite rules in this Stack Overflow answer: https://serverfault.com/a/741197

    Hope that helps.

    Rob

    (@seomandarin)

    Hi @rankmath

    1) I’ve tried putting it in the location block. doesn’t change anything it seems. I always restart nginx after making changes.
    2) I’ve put the error log on debug level, not showing anything from what I can tell.

    ==> /var/log/nginx/domain.com/https_error.log <==
    2020/01/04 00:40:37 [error] 19093#19093: *5 open() "/var/www/domain.com/post-sitemap.xml" failed (2: No such file or directory), client: myipaddress, server: domain.com, request: "GET /post-sitemap.xml HTTP/1.1", host: "domain.com", referrer: "https://domain.com/?sitemap=1"
    
    ==> /var/log/nginx/domain.com/https_access.log <==
    myipaddress - - [04/Jan/2020:00:40:37 +0100] "GET /post-sitemap.xml HTTP/1.1" 404 564 "https://domain.com/?sitemap=1" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ipaddress Safari/537.36"
    
    ==> /var/log/nginx/error.log <==
    2020/01/04 00:33:09 [debug] 18886#18886: accept on 0.0.0.0:80, ready: 0
    2020/01/04 00:33:09 [debug] 18886#18886: posix_memalign: 000055A0C1D9F9F0:512 @16
    2020/01/04 00:33:09 [debug] 18886#18886: *10 accept: myipaddress:50376 fd:27
    2020/01/04 00:33:09 [debug] 18886#18886: *10 event timer add: 27: 60000:570986844
    2020/01/04 00:33:09 [debug] 18886#18886: *10 reusable connection: 1
    2020/01/04 00:33:09 [debug] 18886#18886: *10 epoll add event: fd:27 op:1 ev:80002001
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http wait request handler
    2020/01/04 00:33:09 [debug] 18886#18886: *10 malloc: 000055A0C1CF5030:1024
    2020/01/04 00:33:09 [debug] 18886#18886: *10 recv: eof:0, avail:1
    2020/01/04 00:33:09 [debug] 18886#18886: *10 recv: fd:27 955 of 1024
    2020/01/04 00:33:09 [debug] 18886#18886: *10 reusable connection: 0
    2020/01/04 00:33:09 [debug] 18886#18886: *10 posix_memalign: 000055A0C1DB6B80:4096 @16
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http process request line
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http request line: "GET /sitemap.xml HTTP/1.1"
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http uri: "/sitemap.xml"
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http args: ""
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http exten: "xml"
    2020/01/04 00:33:09 [debug] 18886#18886: *10 posix_memalign: 000055A0C1DAB1E0:4096 @16
    2020/01/04 00:33:09 [debug] 18886#18886: *10 http process request header line
    

    Thanks!

    Plugin Author Rank Math

    (@rankmath)

    Hi @seomandarin

    We’re running out of ideas here. Our next suggestion would be to try another sitemap solution, such as this plugin: https://wordpress.org/plugins/google-sitemap-generator/
    Make sure to deactivate the XML Sitemap module in Rank Math before installing the other plugin.
    After installing and configuring the plugin, please check if the /sitemap.xml file loads correctly. Just like with Rank Math, you will need to add custom rewrite rules in the config file – these should be displayed on the settings page of the plugin.

    Let us know how that goes.

    Plugin Author Rank Math

    (@rankmath)

    Hello,

    Assuming you are using the latest version of the plugin and the issue is not happening, we are closing this topic.

    If that is not the case, please feel free to reply to this topic or open a new one and we would be more than happy to assist.

    Thank you.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Sitemap add rewrite rule nginx’ is closed to new replies.