Yii2 Nginx Elastic Beanstalk 上的高级模板
Posted
技术标签:
【中文标题】Yii2 Nginx Elastic Beanstalk 上的高级模板【英文标题】:Yii2 Advanced Template on Nginx Elastic Beanstalk 【发布时间】:2021-10-28 02:46:02 【问题描述】:我正在尝试在运行 nginx 服务器的 AWS 的 Elastic Beanstalk 堆栈上启动我的 Yii2 高级项目。我一直无法找出允许我访问站点后端的配置。我尝试通过 AWS 文档扩展 nginx 配置:
https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html
这不起作用。所以我从命令行通过 vim 手动修改配置。
同时使用以下配置设置的组合:
https://www.yiiframework.com/wiki/799/yii2-app-advanced-on-single-domain-apache-nginx
无济于事。我已经为后端文件夹尝试了 alias
和 root
,每个变体我都会收到 404(未找到文件)或 502(错误网关)错误。
至少,这是我尝试添加到我的 nginx 配置中的内容:
root /var/www/html/frontend/web;
index index.php index.html index.htm;
location /backend/
root ../../backend/web;
我做错了什么?
【问题讨论】:
再次检查/var/www/html/backend/web
后端的完整路径并使用别名。
试过这个...收到 502 bad gateway 错误。
【参考方案1】:
在这里你可以看到一个完整的 yii2 高级应用示例 nginx 配置。您可以根据需要进行更改。
应用服务器:
mycoolapp.com nginx php7.4-fpm路线:
http://mycoolapp.com -- 前端 http://mycoolapp.com/admin -- 后端 http://mycoolapp.com/api -- apiNginx 配置:
server
## Listen ports config
listen *:80 http2;
#listen *:443 ssl http2;
## Site name config
server_name mycoolapp.com;
## SSL config (uncomment if necessary)
#include /etc/nginx/ssl-snippets/ssl-snippet.conf;
## Access and error log files path
access_log /var/log/nginx/mycoolapp.com.access.log;
error_log /var/log/nginx/mycoolapp.com.error.log;
## Max upload size config
client_max_body_size 32m;
client_body_buffer_size 32m;
charset utf-8;
## Gzip config
gzip on;
gzip_types
text/plain
text/css
application/json
application/x-javascript
text/xml
application/xml
application/xml+rss
text/javascript
application/javascript;
## Path to app root (folder that contains frontend and backend folders)
set $base_root /var/www/html/mycoolapp;
root $base_root;
index index.php index.html;
## Frontend app config
## Entry point: https://mycoolapp.com
location /
# Path to frontend web folder
root $base_root/frontend/web;
try_files $uri $uri/ /frontend/web/index.php$is_args$args;
## Omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
location ~ ^/.+\.(css|less|js|map|ico|png|jpe?g|gif|webp|svg|eot|ttf|woff|woff2|mp4|mov|swf|txt|pdf)$
expires 365d;
log_not_found off;
access_log off;
try_files $uri =404;
## Deny any php file in assets folder (security)
location ~ ^/assets/.+\.php(/|$)
deny all;
## Backend app config
## Entry point: https://mycoolapp.com/admin
location /admin
## Path to backend web folder
root $base_root/backend/web/;
## Redirect to the URL without a trailing slash (uncomment if necessary)
#location = /admin/
# return 301 /admin;
#
## Prevent the directory redirect to the URL with a trailing slash
location = /admin
try_files $uri /backend/web/index.php$is_args$args;
## Omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
location ~ ^/admin/.+\.(css|less|js|map|ico|png|jpe?g|gif|webp|svg|eot|ttf|woff|woff2|mp4|mov|swf|txt|pdf)$
rewrite ^/admin(/.+)$ $1 break;
log_not_found off;
access_log off;
try_files $uri =404;
## Deny any php file in assets folder (security)
location ~ ^/admin/assets/.+\.php(/|$)
deny all;
try_files $uri $uri/ /backend/web/index.php$is_args$args;
## API app config
## Entry point: https://mycoolapp.com/api
location /api
root $base_root/api/web/;
## Redirect to the URL without a trailing slash (uncomment if necessary)
#location = /api/
# return 301 /api;
#
location = /api
try_files $uri /api/web/index.php$is_args$args;
## Omit static files logging, and if they don't exist, avoid processing by Yii (uncomment if necessary)
location ~ ^/api/.+\.(css|less|js|map|ico|png|jpe?g|gif|webp|svg|eot|ttf|woff|woff2|mp4|mov|swf|txt|pdf)$
rewrite ^/api(/.+)$ $1 break;
log_not_found off;
access_log off;
try_files $uri =404;
## Deny any php file in assets folder (security)
location ~ ^/api/assets/.+\.php(/|$)
deny all;
try_files $uri $uri/ /api/web/index.php$is_args$args;
## PHP configuration
location ~ ^/.+\.php(/|$)
## Rewrites
rewrite (?!^/((frontend|api|backend)/web|api|admin))^ /frontend/web$uri break;
rewrite (?!^/api/web)^/api(/.+)$ /api/web$1 break;
rewrite (?!^/backend/web)^/admin(/.+)$ /backend/web$1 break;
## FPM config
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(.*)$;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
try_files $fastcgi_script_name =404;
## Logging and access of restricted folders
location = /robots.txt access_log off; log_not_found off;
location = /favicon.ico access_log off; log_not_found off;
location ~* /CHANGELOG access_log off; log_not_found off; deny all;
location ~* /LICENSE access_log off; log_not_found off; deny all;
location ~* /README access_log off; log_not_found off; deny all;
location ~* /\. access_log off; log_not_found off; deny all;
【讨论】:
我从字面上复制并粘贴了此配置,但仍然没有运气。现在,它甚至不提供页面,只是下载它们。真奇怪。这是一个完整的粘贴箱,我真的很茫然:-(pastebin.com/N9rFASbP 尝试了其他一些方法...每个 URL 都只是重定向到主页。不知道该看什么... 你好,试着把这行fastcgi_pass unix:/run/php/php7.4-fpm.sock;
改成fastcgi_pass unix:/run/php-fpm/www.sock;
同样的问题。所有 URL 都将我带到主页。不确定这是否重要,但我正在运行 php 8。最初,我有 fastcgi_pass php-fpm;
。还有其他建议看吗?以上是关于Yii2 Nginx Elastic Beanstalk 上的高级模板的主要内容,如果未能解决你的问题,请参考以下文章