Nginx:将/sitemap.xml重写为/ sitemap

问题描述 投票:0回答:2

我有一个Codeigniter PHP应用程序,我已经配置了我的nginx conf以获得一些URL。例如,/ index.php / home将重写为/ home。现在我正在尝试配置nginx以将http://mysite.com/sitemap.xml重写为/ sitemap(构造xml文件)。这是我在我的nginx conf中使用的:

            if ($request_uri ~* ^/sitemap.xml)
        {
            rewrite ^/(.*)$ /index.php/sitemap last;
        }

出于某种原因,上述方法不起作用,我认为另一条规则可能存在冲突,但我无法找到解决方法。有人可以帮忙吗? (完整配置文件如下)

       server {
            listen   80;

            root   /www/myapp;
            index index.php index.html index.htm;

            access_log  /var/log/nginx/access.log;

            if (-f /www/myapp/application/errors/error_503.html)
            {
                return 503;
            }

            # canonicalize codeigniter url end points
            #/home will redirect to /
            if ($request_uri ~* ^(/home(/index)?|/index(.php)?)/?$)
            {
                rewrite ^(.*)$ / permanent;
            }

            # removes trailing "index" from all controllers
            if ($request_uri ~* index/?$)
            {
                rewrite ^/(.*)/index/?$ /$1 permanent;
            }

            # removes trailing slashes (prevents SEO duplicate content issues)
            if (!-d $request_filename)
            {
                rewrite ^/(.+)/$ /$1 permanent;
            }

            # removes access to "system" folder, also allows a "System.php" controller
            if ($request_uri ~* ^/system)
            {
                rewrite ^/(.*)$ /index.php?/$1 last;
                break;
            }

############THIS PART IS NOT WORKING
            if ($request_uri ~* ^/sitemap.xml)
            {
                rewrite ^/(.*)$ /index.php/sitemap last;
            }
####################################

            # unless the request is for a valid file (image, js, css, etc.), send to bootstrap
            if (!-e $request_filename)
            {
                rewrite ^/(.*)$ /index.php?/$1 last;
                break;
            }

        ## Default location
        location / {
            root   /www/myapp;
            index  index.html index.htm index.php;
        }       

        ## Images and static content is treated different
        location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico)$ {
          access_log        off;
          expires           30d;
          root /www/myapp;
        }

        ## Parse all .php file in the www directory
        location ~ .php$ {
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            include /etc/nginx/fastcgi_params;
            fastcgi_param  SCRIPT_FILENAME /www/myapp/$fastcgi_script_name;
            fastcgi_param  QUERY_STRING     $query_string;
            fastcgi_param  REQUEST_METHOD   $request_method;
            fastcgi_param  CONTENT_TYPE     $content_type;
            fastcgi_param  CONTENT_LENGTH   $content_length;
            fastcgi_intercept_errors        on;
            fastcgi_ignore_client_abort     off;
            fastcgi_connect_timeout 60;
            fastcgi_send_timeout 180;
            fastcgi_read_timeout 180;
            fastcgi_buffer_size 128k;
            fastcgi_buffers 4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_temp_file_write_size 256k;
        }

        ## catch all
        #error_page 404 /index.php;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        ## Disable viewing .htaccess & .htpassword
        location ~ /\.ht {
            deny  all;
        }
    }
codeigniter nginx rewrite sitemap
2个回答
2
投票

上面的问题是REQUEST_URI仍然是/sitemap.xml而不是/ sitemap,因此CI无法正确路由它。尝试将“last”更改为“redirect”:

        if ($request_uri ~* ^/sitemap.xml)
        {
            rewrite ^/(.*)$ /sitemap redirect;
        }

还可以考虑使用LOCATION而不是IF(一般避免使用IF):

        location = /sitemap.xml
        {
            rewrite .* /sitemap redirect;
        }

希望有所帮助


0
投票

我在我的网站上使用它:

# Sitemap.xml
location /sitemap.xml {
    alias /venvs/mobilepro/src/mobilepro/static/maps/map.xml;
}
© www.soinside.com 2019 - 2024. All rights reserved.