php - AWS EC2 NGINX 上的 PHP 脚本不工作但下载
问题描述
我正在尝试使用 nginx 在 AWS ec2 中设置我的站点。这是我的nginx.conf
:
include /etc/nginx/conf.d/*.conf;
index index.php index.html index.htm;
autoindex off;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name 127.0.0.1;
root /usr/share/nginx/myproject;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
#location / {
#}
location = /index.php {
rewrite ^(.*)$ /app/index.php break;
}
location / {
if (!-e $request_filename){
rewrite ^(.*)$ /app/index.php break;
}
}
location ~ \.(secret|salt|engine|inc|po|sh|bat|cmd|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|inc)$ {
deny all;
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
try_files $uri =404
root /usr/share/nginx/myproject;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
当我访问公共 DNS 时,我得到了下载而不是运行 PHP 脚本。我的 nginx 配置有问题吗?
解决方案
您的rewrite...break
语句应该是rewrite...last
因为 PHP 文件需要在不同的位置进行处理。有关详细信息,请参阅此文档。
例如:
location / {
if (!-e $request_filename){
rewrite ^ /app/index.php last;
}
}
但是,上述功能通常实现为:
location / {
try_files $uri $uri/ /app/index.php;
}
其他问题包括:
语句后面
;
的location ~ \.php$
块中缺少一个。try_files
应该删除块中的
root
语句,因为它是不必要的。location ~ \.php$
推荐阅读
- hyperledger-fabric - 如何使用fabric-sdk-go在现有fabric网络中动态添加组织(或删除组织)?
- selenium - TestNG 没有发送最新的 emailable-report.html
- javascript - Css 图库图片重叠导致浏览器宽度受限
- c - 通过 c 中的方法/函数传递结构的问题
- java - 消除重复输入的最有效方法
- python-2.7 - 在 VScode 上使用 Jython 和 lint
- sql-server - 查询以从 sql 中的链接表中返回值
- angular - 如果用户登录,如何保护登录和注册组件?
- tensorflow - 无效参数:不兼容的形状:[20,2209] vs. [20,4418] in conv1D in feature set
- typescript - 获取复杂类型的补全