nginx add_after_body以及if

问题描述

| Nginx说“ if”语句中不允许使用“ add_after_body”,还有其他选择吗? 这是当前的配置
if ($request_uri ~* ^/(home|page1|page2|page4)(.*+)\\.html$) {
        add_after_body /stats.html;
}
如果我将if块放置在location块中,则会发生相同的问题 这是错误
Reloading Nginx configuration: Nginx: [emerg] \"add_after_body\" directive is not allowed here in /etc/Nginx/sites-enabled/default:22
Nginx: configuration file /etc/Nginx/Nginx.conf test Failed
这是完整的服务器块:
server {

    listen   80; ## listen for ipv4
    #listen   [::]:80; ## listen for ipv6

    server_name .mydoma.in;

    access_log  /var/log/Nginx/access.log;
    error_log   /var/log/Nginx/error.log;

    root   /var/www;

    gzip             on;
    gzip_min_length  1000;
    gzip_proxied     expired no-cache no-store private auth;
    gzip_types       text/plain application/xml;
    gzip_disable     \"MSIE [1-6]\\.\";
    gzip_comp_level  4;

    #if ($request_uri ~* ^/(forum|thread|post|announcement|user|calendar|event|top10)(.*+)\\.html$) {
    #   add_after_body /stats.html;
    #}

    location / {
        index  index.html index.htm index.PHP;

        #MyBB Config

        rewrite ^/forum-([0-9]+)\\.html$ /forumdisplay.PHP?fid=$1;
        rewrite ^/forum-([0-9]+)-page-([0-9]+)\\.html$ /forumdisplay.PHP?fid=$1&page=$2;
        rewrite ^/thread-([0-9]+)\\.html$ /showthread.PHP?tid=$1;
        rewrite ^/thread-([0-9]+)-page-([0-9]+)\\.html$ /showthread.PHP?tid=$1&page=$2;
        rewrite ^/thread-([0-9]+)-lastpost\\.html$ /showthread.PHP?tid=$1&action=lastpost;
        rewrite ^/thread-([0-9]+)-nextnewest\\.html$ /showthread.PHP?tid=$1&action=nextnewest;
        rewrite ^/thread-([0-9]+)-nextoldest\\.html$ /showthread.PHP?tid=$1&action=nextoldest;
        rewrite ^/thread-([0-9]+)-newpost\\.html$ /showthread.PHP?tid=$1&action=newpost;
        rewrite ^/thread-([0-9]+)-post-([0-9]+)\\.html$ /showthread.PHP?tid=$1&pid=$2;

        rewrite ^/post-([0-9]+)\\.html$ /showthread.PHP?pid=$1;

        rewrite ^/announcement-([0-9]+)\\.html$ /announcements.PHP?aid=$1;

        rewrite ^/user-([0-9]+)\\.html$ /member.PHP?action=profile&uid=$1;

        rewrite ^/calendar-([0-9]+)\\.html$ /calendar.PHP?calendar=$1;
        rewrite ^/calendar-([0-9]+)-year-([0-9]+)\\.html$ /calendar.PHP?action=yearview&calendar=$1&year=$2;
        rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)\\.html$ /calendar.PHP?calendar=$1&year=$2&month=$3;
        rewrite ^/calendar-([0-9]+)-year-([0-9]+)-month-([0-9]+)-day-([0-9]+)\\.html$ /calendar.PHP?action=dayview&calendar=$1&year=$2&month=$3&day=$4;
        rewrite ^/calendar-([0-9]+)-week-(n?[0-9]+)\\.html$ /calendar.PHP?action=weekview&calendar=$1&week=$2;

        rewrite ^/event-([0-9]+)\\.html$ /calendar.PHP?action=event&eid=$1;

        rewrite ^/sitemap\\-([^./]+)\\.xml$ /misc.PHP?google_SEO_sitemap=$1;

        #if ($request_uri ~* \"^/(forum|thread|post|announcement|user|calendar|event|top10)\") {
            #        add_after_body /stats.html;
        #}

    }


    location ~ \\.PHP$ {
        fastcgi_split_path_info ^(.+\\.PHP)(/.+)$;
        include /etc/Nginx/fastcgi_params;
        fastcgi_intercept_errors on;
        fastcgi_pass PHP;
    }

    #expires 1M;

    # Static assets
    location ~* ^.+\\.(manifest|appcache)$ {
        expires -1;
    }

    # Set expires max on static file types
    location ~* ^.+\\.(css|js|jpg|jpeg|gif|png|ico|gz|svg|svgz|ttf|otf|woff|eot|mp4|ogg|ogv|webm)$ {
        expires max;
        access_log off;
    }

    # opt-in to the future
    add_header \"X-UA-Compatible\" \"IE=Edge,chrome=1\";

    location = /50x/ {
        random_index on;
    }

    error_page 500 502 503 504  /50X/;
}
我试过将语句同时放在
location
语句的内部和外部(
add_after_body
在ѭ3or块或
server
块中都没有
if
语句的情况下工作)     

解决方法

        您可以使用它。似乎合适。
location ~ (forumdisplay|showthread|announcements|member|calendar\\.php$) {
  fastcgi_split_path_info ^(.+\\.php)(/.+)$;
  include /etc/nginx/fastcgi_params;
  fastcgi_intercept_errors on;
  fastcgi_pass php;
  add_after_body /stats.html;
}