我正在尝试为 Codeigniter 设置重定向,以便 URL 像localhost/codeigniter/class, 所以 localhost/codeigniter/class. Drupal 8 中也使用了这种类型的 URL,所以我决定使用 regexp 进行重定向:
location ~ /(drupal|codeigniter)/ {
try_files $uri $uri/ @rewrite8;
}
location @rewrite8 {
rewrite ^/(drupal|codeigniter) /$1/index.php;
}
但是,当我转到该页面时,系统会提示我下载文件(在响应标头中,MIME 类型看起来像octet-stream)。
如果您不使用正则表达式:
location /codeigniter/ {
try_files $uri $uri/ @rewrite8;
}
然后一切正常并给出text/html。
什么可能导致这种情况以及如何进行正确的重定向?
UPD。完整配置:
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html index.php;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
location /codeigniter/ {
try_files $uri $uri/ @rewrite8;
}
location /drupal7/ {
try_files $uri $uri/ @rewrite7;
}
location @rewrite7 {
rewrite ^/drupal7/(.*)$ /drupal7/index.php?q=$1;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php(/|$) {
fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_pass unix:/var/run/php-fastcgi/php-fastcgi.socket;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
}
location @rewrite8 {
rewrite ^/(drupal|codeigniter) /$1/index.php;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
阅读 nginx 如何处理位置http://nginx.org/ru/docs/http/ngx_http_core_module.html#location
在您的情况下,在更改之前,请求
/codeigniter/index.php已在 中处理location ~ \.php,因为location正则表达式给出的请求“赢得”了正则前缀location /codeigniter/。更改后,两者都
location成为正则表达式,在这种情况下,第一个匹配的获胜,即location ~ /(drupal|codeigniter)/.nginx 的作者自己相信(我同意他们的观点),如果你能用几行代码搞定,就没有必要用常规来复杂化配置。
如果你仍然想,那么首先,交换这些
location地方,其次,将新块更正为location ~ ^/(drupal|codeigniter)/