英文:
Why does php only work in some directories of my Nginx webserver?
问题 {#heading}
I had my Nginx web server set up just the way I needed it to work - but now I need it to work in a sub directory of my server - and, perhaps more importantly, on php which is embedded in html.
我的Nginx web服务器已经按照我需要的方式设置好了 - 但现在我需要它在服务器的子目录中工作 - 而且可能更重要的是,要在嵌入在HTML中的PHP上工作。
My server is all based on Docker containers, with a separate container for php, for nginx and for mariadb.
我的服务器都基于Docker容器构建,有一个专门用于php的容器,一个用于nginx的容器,还有一个用于mariadb的容器。
The directory structure (outside of the containers, where all the served files live) is as follows...
目录结构(在容器之外,所有提供的文件都位于其中)如下所示...
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
htdocs
|--Common (我的CSS、JS等所在的地方)
|--Resources (图像和视频资源所在的地方)
|--labs (我进行实验的地方)
| |--ExperimentSite
|--blog (我写博客的地方)
|--forum (论坛)
|--family
I've tried .php files in root (that works file), in WordPress (also fine) and Vanilla (fine too) - but when I try to load a .php file in /Labs/ExperimentSite it downloads the .php rather than running it. Also, I can't run .php embedded within HTML.
我尝试在根目录中使用.php文件(这样可以正常工作),在WordPress中(也可以正常工作)以及Vanilla中(也可以正常工作) - 但是当我尝试加载/Labs/ExperimentSite中的.php文件时,它会下载.php文件而不是运行它。此外,我无法运行嵌入在HTML中的.php文件。
I feel sure that the problem must be with my site .conf file, but even after much experimentation I can't get it to work (although I have worked out how to break my website quite a lot!)
我确信问题肯定出在我的站点.conf文件中,但即使经过多次实验,我也无法让它正常工作(尽管我已经弄清楚了如何破坏我的网站很多次!)
What have I missed here to get my /labs/ExperimentSite working? How can I get php working in html (I tried changing location ~ \.php$
to location ~ \.(php|htm|html)$
but that just broke everything!)
我在这里漏掉了什么,以使我的/Labs/ExperimentSite正常工作?如何使php在html中工作(我尝试将 location ~ \.php$
更改为 location ~ \.(php|htm|html)$
,但那只是使一切都崩溃了!)
英文:
I had my Nginx web server set up just the way I needed it to work - but now I need it to work in a sub directory of my server - and, perhaps more importantly, on php which is embedded in html.
My server is all based on Docker containers, with a separate container for php, for nginx and for mariadb.
The directory structure (outside of the containers, where all the served files live) is as follows...
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
I've tried .php files in root (that works file), in WordPress (also fine) and Vanilla (fine too) - but when I try to load a .php file in /Labs/ExperimentSite it downloads the .php rather than running it. Also, I can't run .php embedded within HTML.
I feel sure that the problem must be with my site .conf file, but even after much experimentation I can't get it to work (although I have worked out how to break my website quite a lot!)
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;
}
`}
`
What have I missed here to get my /labs/ExperimentSite working? How can I get php working in html (I tried changing location ~ \.php$
to location ~ \.(php|htm|html)$
but that just broke everything!)
答案1 {#1}
得分: 2
你在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)$
。
英文:
You have used the ^~
operator in location ^~ /labs/ExperimentSite
to prevent any other location causing the "Restricted Content" to be bypassed.
Unfortunately, this also prevents the location ~ \.php$
block from handling any request that ends with .php
and sending it to the PHP processor.
To handle .php
files within /labs/ExperimentSite
you could use a nested location.
For example:
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;
...
}
`}
`
To send .html
files to PHP, you should change the regular expression from \.php$
to \.(php|html)$
.