- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在 nginx 1.4.1 上使用 PHP-FastCGI 运行服务器。目前我已对其进行设置,以便它从我的 URL 中删除尾随斜杠并发出 301 重定向。但是,当我访问存在的目录时,我被迫进入重定向循环。我当前的文档根目录如下所示:
- index.php (app)
- webgrind
- index.php
- static
- css
目前我无法访问 example.com/webgrind 或任何其他目录。我的访问日志反复显示类似于:
GET /webgrind/ HTTP/1.1" 301 178 "-"
GET /webgrind HTTP/1.1" 301 178 "-"
这是我的 nginx.conf 中的服务器 block :
server {
listen 80;
server_name example.com;
location / {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
}
rewrite ^/(.*)/$ /$1 permanent;
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
root /var/www/example/public;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
我知道 rewrite ^/(.*)/$/$1 permanent;
是有问题的行。如果我删除它并访问 example.com/webgrind,则会发出 301 重定向到 example.com/webgrind/,因为它是一个目录。但是,我的应用程序现在将接受尾随斜杠和非尾随斜杠(即 example.com/users/和 example.com/users),但这不是我想要的。
按如下方式将“if”指令包裹在我的重写周围仍然会为我的目录创建重定向循环(显然, if is evil ,但在这种情况下重写指令被认为是安全的):
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
(我知道访问 webgrind/index.php 可以解决我的问题,但是当我的生产目录实时推送时,我希望避免昂贵且不专业的重定向循环。)
那么,如何有条件地仅删除不存在的资源(我的 Web 应用程序路径)的尾部斜杠?
更新:我的(未更改的)fastcgi_params 配置:
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_param SCRIPT_FILENAME $request_filename;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
fastcgi_param HTTPS $https;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200;
最佳答案
将 root
指令放在 location
block 之外作为 server
block 的直接子级解决了该问题。
server {
listen 80;
server_name example.com;
# This WORKS!
root /var/www/example/public;
location / {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
}
if (!-d $request_filename) {
rewrite ^/(.*)/$ /$1 permanent;
}
location = /favicon.ico {
access_log off;
log_not_found off;
}
location ~ \.php$ {
try_files $uri $uri/ /index.php?$args;
index index.php index.html index.htm;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /var/www/example/public$fastcgi_script_name;
fastcgi_param APPLICATION_ENV testing;
fastcgi_param PATH /usr/bin:/bin:/usr/sbin:/sbin;
fastcgi_intercept_errors on;
include fastcgi_params;
}
}
显然这是一个pitfall Nginx wiki 建议避免。
关于redirect - 仅当目录不存在时,如何从 nginx 中的 URL 中删除尾部斜杠?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18422092/
我有一个右下角倾斜的元素,我必须在其上放置一个盒子阴影。有时倾斜的 Angular 被徽章覆盖 - 我的问题不适用,如果是这样的话: 这是信息框及其边 Angular 的 (s)css 部分(还有更多
是否可以在纯 html/css 中创建类似下面的内容? 我想做这个响应式和全 (100%) 宽度(最大左 Angular 100 像素,右边最小 50 像素,类似的东西)。 最佳答案 您可以通过转换(
如何在 fabricjs 文本中为文本提供渐变或斜 Angular 效果?? http://fabricjs.com/fabric-intro-part-2/ 这里给出了形状和所有示例,我将其与文本绑
我用过: http://apps.eky.hk/css-triangle-generator/ 为彼此对 Angular 放置的两个不等边三 Angular 形生成 css: 左下三 Angular
我是一名优秀的程序员,十分优秀!