首页 > 解决方案 > 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.phpor dist.me.com/index2.php,我会得到错误404 Not Found

我尝试更改文件的一些值,conf但对我没有帮助。

两者都index.php存在index2.php/var/www/html路径中。

标签: dockernginx

解决方案


您现有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;
}

推荐阅读