设置:QGis-2.18 服务器(实际上是一个嵌入式地图服务器) 实例通过 Ubuntu 上 Apache-2.4.18 中的 FastCGI。
如果在 cgi 处理程序的查询字符串中设置了某个值,我想添加另一个值。为此,我在顶部添加了三行/etc/apache2/conf-enabled/qgis.conf
:
RewriteEngine on
RewriteCond "%{QUERY_STRING}" "application/json" [NC]
RewriteRule "^/$" "/?OUTPUTFORMAT=GeoJSON" [PT,QSA]
ScriptAlias / /usr/lib/cgi-bin/qgis_mapserv.fcgi
<Location "/">
SetHandler fcgid-script
Require all granted
PassEnv QGIS_PROJECT_FILE
</Location>
FcgidInitialEnv QGIS_LOG_FILE ${QGIS_LOG_FILE}
FcgidInitialEnv QGIS_SERVER_LOG_FILE ${QGIS_SERVER_LOG_FILE}
FcgidInitialEnv QGIS_DEBUG ${QGIS_DEBUG}
FcgidInitialEnv QGIS_SERVER_LOG_LEVEL ${QGIS_SERVER_LOG_LEVEL}
FcgidInitialEnv QGIS_PLUGINPATH "${QGIS_PLUGINPATH}"
FcgidInitialEnv PGSERVICEFILE ${PGSERVICEFILE}
FcgidInitialEnv HOME /var/www
我像这样访问服务器:
http://myserver.invalid.tld:51081/
?SERVICE=WFS
&VERSION=1.1.0
&REQUEST=GetFeature
&OUTPUTFORMAT=application/json
&MAXFEATURES=1
&SRSNAME=EPSG:4326
&TYPENAME=feature_type_name
&BBOX=8.5985658,56.447691,8.600106,56.448553
&OUTPUTFORMAT=GeoJSON
我原本以为效果会和手动添加到URL 末尾一样,但是我看不出有什么区别重新启动 apache 后。(是的,我已经运行了sudo a2enmod rewrite
。)
我不确定重写规则和脚本别名之间的交互是如何进行的,所以我想其中一个会遮蔽另一个?不幸的是,我也不知道如何调试它。
该服务器启用了一个虚拟主机,对我来说它看起来像 OOTB 配置:
# apache2ctl -S
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 172.17.0.10. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80 172.17.0.10 (/etc/apache2/sites-enabled/000-default.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/proc/self/fd/2"
Mutex default: dir="/var/lock/apache2" mechanism=fcntl
Mutex fcgid-pipe: using_defaults
Mutex watchdog-callback: using_defaults
Mutex rewrite-map: using_defaults
Mutex fcgid-proctbl: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33
以下是/etc/apache2/sites-enabled/000-default.conf
(删除了评论):
<VirtualHost *:80>
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ErrorLog /proc/self/fd/2
CustomLog /proc/self/fd/1 combined
</VirtualHost>
最后apache2.conf
(再次删除评论):
Mutex file:${APACHE_LOCK_DIR} default
PidFile ${APACHE_PID_FILE}
Timeout 300
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
User ${APACHE_RUN_USER}
Group ${APACHE_RUN_GROUP}
HostnameLookups Off
ErrorLog /proc/self/fd/2
LogLevel warn
IncludeOptional mods-enabled/*.load
IncludeOptional mods-enabled/*.conf
Include ports.conf
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
AccessFileName .htaccess
<FilesMatch "^\.ht">
Require all denied
</FilesMatch>
LogFormat "%v:%p %h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" vhost_combined
LogFormat "%h %l %u %t \"%r\" %>s %O \"%{Referer}i\" \"%{User-Agent}i\"" combined
LogFormat "%h %l %u %t \"%r\" %>s %O" common
LogFormat "%{Referer}i -> %U" referer
LogFormat "%{User-agent}i" agent
IncludeOptional conf-enabled/*.conf
IncludeOptional sites-enabled/*.conf
(为了加分,我想代替 application/json
在查询字符串中使用GeoJSON
,但如果我可以将参数附加到开头,我就接近目标了。)
答案1
我原本期望效果与我手动添加的效果相同
&OUTPUTFORMAT=GeoJSON
好吧,这也许是“问题”所在;但不是附加。请求中的原始查询字符串是附加。替换字符串中的查询字符串 ( OUTPUTFORMAT=GeoJSON
) 位于查询字符串的开头。因此,根据您如何读取/解析查询字符串中的参数,您的新设置可能会被覆盖。
QUERY_STRING
要专门将某些内容附加到现有查询字符串,您可以使用代换(而不是使用标志QSA
)。例如:
RewriteCond %{QUERY_STRING} application/json
RewriteRule ^/$ /?%{QUERY_STRING}&OUTPUTFORMAT=GeoJSON [PT]
无需将每个参数括在引号中,除非它们包含空格。仅NC
当您特别需要不区分大小写的匹配时才使用此标志。
或者,代替请求中的URLOUTPUTFORMAT=application/json
参数,那么你可以这样做:
RewriteCond %{QUERY_STRING} (.*)OUTPUTFORMAT=application/json(.*)
RewriteRule ^/$ /?%1OUTPUTFORMAT=GeoJSON%2 [PT]
%1
并%2
反向引用前面捕获的组条件模式. 即一切前和后查询字符串中的原始 URL 参数。