gpt4 book ai didi

frameworks - NGINX 配置。带有 PATHINFO 404 的 PHP 框架

转载 作者:搜寻专家 更新时间:2023-10-31 21:12:54 25 4
gpt4 key购买 nike

我平时用apache,想试试NGINX。

我已将它安装在我的 ubuntu 开发机器上,并设置和开发了一些不同的框架和站点(codeigniter、symfony、laravel 等)。

我遇到的问题是只有以 .php 结尾的路径才有效。如果我尝试 index.php/welcome/index 它只是 404s 而不是加载 index.php。

我试过将 cgi.fix_pathinfo 设置为 1 和 0。

这是我当前的(许多尝试过的)站点配置。

server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default_server ipv6only=on; ## listen for ipv6

root /my/path;
index index.php index.html;

# Make site accessible from http://localhost/
server_name localhost;

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
#error_page 500 502 503 504 /50x.html;
#location = /50x.html {
# root /usr/share/nginx/www;
#}

location ~ \.php$ {
try_files $uri =404;

# Fix for server variables that behave differently under nginx/php-fpm than typically expected
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# Include the standard fastcgi_params file included with nginx
include fastcgi_params;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_index index.php;
# Override the SCRIPT_FILENAME variable set by fastcgi_params
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Pass to upstream PHP-FPM; This must match whatever you name your upstream connection
fastcgi_pass unix:/var/run/php5-fpm.sock;
}


location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}

最佳答案

我更愿意使用以下 nginx 配置结构。它更干净:

location / {
try_files $uri $uri/ @phpsite;
}

location @phpsite {
include fastcgi_params;
... other fast_cgi directives
}

可以在流行的 silex 项目中找到更复杂的设置:http://silex.sensiolabs.org/doc/web_servers.html#nginx .

我在原始配置文件中看到 2 个问题:

location ~ \.php$ {
try_files $uri =404;
...
}
  1. 在正则表达式中,'$' 表示匹配字符串的末尾。因此,如 prodigitalson 的评论所述,它失败了。
  2. 上面的 fast_cgi 位置 block 中的 try_files 指令不应该存在,因为该位置 block 应该由 php 单独处理。删除那条线会更干净。

关于frameworks - NGINX 配置。带有 PATHINFO 404 的 PHP 框架,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15316742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com