docker - nginx 仅服务/但不服务任何其他文件
问题描述
我有两个码头集装箱如下:
nginx ==> working as proxy web server (nginx web server)
dist ==> working as a php-fpm container
这是我的dist.conf
:
server {
server_name dist.me.com;
root /var/www/html;
location / {
# try to serve file directly, fallback to index.php
try_files $uri /index.php$is_args$args;
}
location ~ ^/index\.php(/|$) {
fastcgi_pass dist:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
internal;
}
location ~ \.php$ {
return 404;
}
error_log /var/log/nginx/dist_error.log;
access_log /var/log/nginx/dist_access.log;
}
问题是如果我输入dist.me.com
,它会显示我的index.php
内容。但是如果我输入dist.me.com/index.php
or dist.me.com/index2.php
,我会得到错误404 Not Found。
我尝试更改文件的一些值,conf
但对我没有帮助。
两者都index.php
存在index2.php
于/var/www/html
路径中。
解决方案
您现有dist.conf
的旨在阻止以.php
. 该internal
指令防止直接访问某个位置,并且还有一个位置显式返回 404 用于任何以 . 结尾的 URI .php
。
您需要更改location
规则,删除internal
指令,并删除location
它后面的块。
例如,将location ~ ^/index\.php(/|$) { ... }
and替换location ~ \.php$ { ... }
为单个location
块,如下所示:
location ~ \.php(/|$) {
fastcgi_pass dist:9000;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
fastcgi_param DOCUMENT_ROOT $realpath_root;
}
推荐阅读
- immutable.js - 不可变的 mergeIn 覆盖原始值
- visual-studio-code - vscode:会话数据(恢复打开的文件)存储在哪里?可以使用相对路径吗?
- mongodb - 如何更新文档网络中数组中的字段
- javascript - 我的 Nativescript http fetch 无法正确解析
- node.js - Twillio 错误 30004“消息传递 - 消息被阻止”
- codeigniter - 使用我的另一个自定义路由时,Codeigniter 路由不起作用
- python - 将图像转换为像素数组
- javascript - Phaser 3 中的旋转约束
- emacs - Emacs Tramp Mode 在使用编译模式时卡住
- networking - 完全加载 mp3 文件时的 amp-audio 问题