nginx:String用连字符replace加号

我正在尝试重写数百个url,除了一个场景外,我已经非常接近了。

有些url是这样的:

/city/?sl_city=CityName&directory_nonce=45ad967e02 

但是有些在sl_city查询sl_city有一个+作为单词分隔符。 那些需要改变为-

 /city/?sl_city=Some+City&directory_nonce=45ad967e02 

这两个url的最终结果应如下所示:

 /locations/cityname /locations/some-city 

我的基本重写规则是这样的:

 location ~* ^/city/(.+)? { if ($args ~* "sl_city=(.+)&directory_nonce=(.+)") { set $city $1; set $args ''; rewrite ^.*$ /locations/$city permanent; } } 

但我的第二个URL 404s(回来/locations/Some+City ),但locations/Some-City解决。 目前我并不担心这些URL。

基本上我想检查$city之前,它做了最后的永久重写,任何+find,将其更改为-

我所看到的所有例子都涉及安装另一个模块,我认为我不能这样做。 而我迄今为止的尝试没有奏效。

编辑

这是我安装的模块。 试图找出是否ngx_http_substitutions_filter_module做我所需要的,但不知道。

 nginx version: nginx/1.8.0 built with OpenSSL 1.0.1f 6 Jan 2014 TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_spdy_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-auth-pam --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-dav-ext-module --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-echo --add-module=/build/buildd/nginx-1.8.0/debian/modules/nginx-upstream-fair --add-module=/build/buildd/nginx-1.8.0/debian/modules/ngx_http_substitutions_filter_module 

这将replace为你的连字符:

 location ^~ /city/ { # For the single word case, just copy the city name set $city $arg_sl_city; # If the city is two words, convert + to "-" if ($arg_sl_city ~* "(\w+)[-+](\w+)") { set $city "$1-$2"; } set $args ''; rewrite ^.*$ /locations/$city permanent; } 

但是,降低城市名称需要像embedded式Perl这样的模块 。 也就是说,除非所有可能的城市名称都是已知的,并且大小合适的列表。 然后,您可以使用map指令将大写版本静态映射到小写版本。