首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Nginx:将/sitemap.xml重写为/sitemap

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

Stack Overflow用户
提问于 2011-11-18 14:04:04
回答 2查看 9.5K关注 0票数 0

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

代码语言:javascript
运行
复制
            if ($request_uri ~* ^/sitemap.xml)
        {
            rewrite ^/(.*)$ /index.php/sitemap last;
        }

由于某些原因,上面的规则不起作用,我认为另一个规则可能会冲突,但我想不出解决办法。有人能帮上忙吗?(完整的配置文件如下)

代码语言:javascript
运行
复制
       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;
        }
    }
EN

回答 2

Stack Overflow用户

发布于 2011-11-18 16:07:31

上面的问题是REQUEST_URI仍然是/sitemap.xml而不是/sitemap,所以CI不能正确地路由它。尝试将"last“更改为"redirect":

代码语言:javascript
运行
复制
        if ($request_uri ~* ^/sitemap.xml)
        {
            rewrite ^/(.*)$ /sitemap redirect;
        }

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

代码语言:javascript
运行
复制
        location = /sitemap.xml
        {
            rewrite .* /sitemap redirect;
        }

希望这能有所帮助

票数 2
EN

Stack Overflow用户

发布于 2017-09-29 22:12:00

我在我的网站上使用它:

代码语言:javascript
运行
复制
# Sitemap.xml
location /sitemap.xml {
    alias /venvs/mobilepro/src/mobilepro/static/maps/map.xml;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8178404

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档