为什么php只能在我的Nginx服务器的某些目录下工作?

kx1ctssn  于 2023-10-17  发布在  Nginx
关注(0)|答案(1)|浏览(145)

我有我的Nginx Web服务器设置只是我需要它的工作方式-但现在我需要它在我的服务器的子目录工作-也许更重要的是,在PHP上嵌入在HTML中。
我的服务器都是基于Docker容器的,有一个单独的PHP容器,Nginx和mariadb。
目录结构(容器之外,所有服务的文件都在其中)如下所示.

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

我试过在根目录下安装.php文件(这个文件可以用),在Wordpress(也可以用)和Vanilla(也可以用)-但是当我试图在/Labs/ExperimentSite中加载.php文件时,它会下载.php文件而不是运行它。另外,我不能运行嵌入在HTML中的.php。
我确信问题一定出在我的site .conf文件上,但即使经过多次实验,我也无法让它正常工作(尽管我已经想出了很多方法来破坏我的网站!)

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;
        }

}

我在这里错过了什么让我的/labs/ExperimentSite工作?我怎样才能让php在html中工作(我试着把location ~ \.php$改为location ~ \.(php|htm|html)$,但这破坏了一切!))

svgewumm

svgewumm1#

您已在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)$

相关问题