我已经按照我需要的方式设置了我的nginx网络服务器,但现在我需要它在我的服务器的子目录下工作,也许更重要的是,在嵌入到html中的php上工作.

我的服务器完全基于Docker容器,为php、nginx和MariaDB提供了单独的容器.

目录 struct (容器外部,所有提供的文件都在其中)如下所示的…

htdocs
|--Common     (where my CSS, JS, etc is)
|--Resources  (where image and video assets are)
|--labs       (where I experiment)
|  |--ExperimentSite
|--blog       (where I blog)
|--forum      (Forums)
|--family

我试过在根目录(也可以)、Wordpress(也很好)和Vanilla(也很好)中使用.php文件--但当我试图在/Labs/ExperientSite中加载.php文件时,它会下载.php文件,而不是运行它.此外,我不能运行嵌入在HTML中的.php.

我确信问题一定出在我的站点.conf文件上,但即使经过多次试验,我也无法使其正常工作(尽管我已经知道如何多次 destruct 我的网站!)

server {
        listen 0.0.0.0:8080;
        server_name  mywebsitename_placeholder.com www.mywebsitename_placeholder.com;
        return 301 https://$server_name$request_uri;
}

server {
        listen 0.0.0.0:8443;
        server_name  mywebsitename_placeholder.com www.mywebsitename_placeholder.com;
        ssl_certificate     bitnami/certs/www.mywebsitename_placeholder.com.crt;
        ssl_certificate_key bitnami/certs/www.mywebsitename_placeholder.com.key;
        server_tokens off;
        
        error_log "/opt/bitnami/nginx/logs/mywebsitename_placeholder-error.log";
        access_log  "/opt/bitnami/nginx/logs/mywebsitename_placeholder-access.log";

        error_page 403 /forbidden.html;
        error_page 404 /lost.html;

        location / {
                try_files $uri $uri/main.html;
        }

        location ~ \.php$ {
        # fastcgi_pass [PHP_FPM_LINK_NAME]:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_pass phpfpm-mywebsitename:9000;
                fastcgi_index index.php;
                include fastcgi.conf;
                root /app;
                error_page 401 /badpassword.php;
                error_page 403 /forbidden.html;
                error_page 404 /lost.html;
                fastcgi_intercept_errors on;
        }

        location ~ \.html$ {
                root   /app/mywebsitename/htdocs;
        }
 
        location ~* \.(js|css|png|jpg|jpeg|gif|ico|dmg|zip|svg|xml)$ {
                expires max;
                log_not_found off;
                root   /app/mywebsitename/htdocs;
        }
        
        location /blog {
                try_files $uri $uri/ /blog/index.php?$args;
        }

        location /forum {
                try_files $uri @vanilla;
        }

        location @vanilla {
                rewrite ^/forum(/.*) /forum/index.php?p=$1&$args last;
        }

        location ~ \.php$ {
                fastcgi_split_path_info ^(/)(/.*)$;
        }

        location ^~ /family {
                root   /app/mywebsitename/htdocs;
                index  index.html;
                auth_basic "Restricted Content";
                auth_basic_user_file /app/.htpasswd;
        }

        location ^~ /labs/ExperimentSite {
                root   /app/mywebsitename/htdocs;
                index  index.html;
                auth_basic "Restricted Content";
                auth_basic_user_file /app/.htpasswd;
                
                try_files $uri $uri/ /labs/ExperimentSite/index.html?$args;
        }

}

为了让我的/实验室/实验站点正常工作,我在这里错过了什么?我怎样才能让php在html中工作(我试着把location ~ \.php$改成location ~ \.(php|htm|html)$,但这完全 destruct 了我的 idea !)

推荐答案

您已经在location ^~ /labs/ExperimentSite中使用了^~操作符,以防止任何其他位置导致"受限内容"被绕过.

遗憾的是,这也阻止了location ~ \.php$块处理任何以.php结尾的请求并将其发送到PHP处理器.

要处理/labs/ExperimentSite内的.php个文件,您可以使用嵌套位置.

例如:

location ^~ /labs/ExperimentSite {
    root   /app/mywebsitename/htdocs;
    index  index.html;
    auth_basic "Restricted Content";
    auth_basic_user_file /app/.htpasswd;
    
    try_files $uri $uri/ /labs/ExperimentSite/index.html?$args;

    location ~ \.php$ {
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass phpfpm-mywebsitename:9000;
        ...
    }
}

要将.html个文件发送到PHP,您应该将正则表达式从\.php$更改为\.(php|html)$.

Php相关问答推荐

PHP -使用preg_match在字符串中进行匹配

在WooCommerce中执行一次银行电汇确认付款代码

PHP如何将ARRAY_MAP与Reset()配合使用

如何修复警告:当使用WordPress get_the_Terms()时,Foreach()参数必须是ARRAY|OBJECT类型?

添加登录:需要app.yaml仍然允许所有拥有Google帐户的人访问

莫德克斯.当我try 使用pThumb为手机压缩图像时,没有任何react

WooCommerce短代码,显示特定产品的购物车徽章中的当前数量

升级到PHP 8.2后无法删除cURL Cookie文件

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

根据小计向 WooCommerce Checkout 添加佣金

htaccess 重定向子域错误的 url

由于 PHP 版本不受支持,如何终止 PHP 脚本?

在 WooCommerce 我的帐户自定义部分显示未购买的产品

后退按钮不起作用 laravel ajax crud

PHP header() 是否缓存重定向,如果是,如何防止它这样做?

为什么在 phpunit 的开头测试 PHP_VERSION?

正则表达式抓取递归括号的内容

htaccess 无限循环问题

如何在 php/laravel 中获取元素在数组中的位置

路由未定义的 laravel 导出