try_files指令的作用是依次尝试匹配请求的uri作为文件、目录,若均不存在则将请求内部重写到index.php,使yii框架能接管路由处理,实现“美观url”;2. 确保yii应用正确处理静态资源和php脚本的方法是:通过root指令指定web目录,使用location ~ .php$块将php请求通过fastcgi_pass转发给php-fpm,并设置fastcgi_param script_filename确保脚本路径正确,同时为静态资源配置独立的location块以启用缓存、关闭日志等优化;3. 常见错误包括root路径错误、php-fpm未运行、try_files缺失、权限不足和script_filename配置错误,性能优化建议包括启用gzip压缩、配置静态文件缓存、优化php-fpm进程设置、启用opcache、合理使用nginx fastcgi缓存、限制日志记录及调整client_max_body_size以支持大文件上传,所有配置修改后需通过nginx -t测试语法并重新加载服务生效。

YII框架的Nginx配置核心在于将所有非文件或非目录的请求重写到
index.php
server {
listen 80;
server_name your_domain.com; # 替换为你的域名或IP地址
root /path/to/your/yii/app/web; # 替换为你的Yii应用web目录的绝对路径
index index.php;
# 允许访问隐藏文件,但通常不建议在生产环境直接暴露
# location ~ /\.ht {
# deny all;
# }
# 禁止直接访问composer.json等敏感文件
location ~* /(protected|framework|config|common|runtime|vendor|console|commands|models|views|controllers|modules|widgets)/ {
deny all;
}
# 处理所有非文件/非目录的请求,将其重写到index.php
location / {
try_files $uri $uri/ /index.php?$args;
}
# 将所有.php文件的请求转发给PHP-FPM
location ~ \.php$ {
# 如果你的PHP-FPM是监听Unix socket
# fastcgi_pass unix:/var/run/php/php-fpm.sock;
# 如果你的PHP-FPM是监听TCP端口
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params; # 包含Nginx默认的fastcgi参数
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_buffer_size 128k;
fastcgi_buffers 4 256k;
fastcgi_busy_buffers_size 256k;
}
# 优化静态文件缓存,可以根据实际情况调整缓存时间
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|ttf|woff|woff2)$ {
expires 30d; # 缓存30天
add_header Cache-Control "public, must-revalidate";
access_log off; # 静态文件访问量大时可以关闭日志
}
# 错误页面配置
error_page 404 /index.php; # 或者自定义的404页面
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# root /usr/share/nginx/html;
# }
# 开启Gzip压缩,提升传输效率
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
}try_files
try_files
具体来说,
try_files $uri $uri/ /index.php?$args;
尝试查找文件 ($uri
/about/us
web
about/us
robots.txt
favicon.ico
尝试查找目录 ($uri/
web
about/us
index.php
index
回退到index.php
/index.php?$args
/index.php
?$args
?id=123
index.php
为什么这对Yii至关重要呢?因为Yii框架的URL管理器(
UrlManager
index.php
your_domain.com/product/view?id=10
product/view
try_files
index.php
/product/view
ProductController
actionView
id=10
try_files
确保Yii应用在Nginx下正确处理静态资源和PHP脚本,这涉及到Nginx配置中的几个关键点,理解它们的工作原理能让你少走很多弯路。
处理PHP脚本的核心在于
location ~ \.php$
fastcgi_pass
.php
location
fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php/php-fpm.sock;
index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
$document_root
server
root
web
$fastcgi_script_name
至于静态资源(如CSS、JavaScript、图片等),它们的处理相对简单直接。Nginx本身就是高性能的静态文件服务器,所以我们希望它能直接处理这些文件,而不要把它们也丢给PHP-FPM。
首先,
root /path/to/your/yii/app/web;
/css/site.css
/path/to/your/yii/app/web/css/site.css
为了进一步优化,你可以添加一个专门的
location
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|ttf|woff|woff2)$ {
expires 30d; # 缓存30天
add_header Cache-Control "public, must-revalidate";
access_log off; # 静态文件访问量大时可以关闭日志
}这个块的作用是:
)**: 它使用正则表达式匹配所有以常见静态文件扩展名结尾的请求。
expires 30d;
add_header Cache-Control "public, must-revalidate";
access_log off;
通过这样的配置,Nginx能够高效地区分并处理不同类型的请求:PHP脚本交给PHP-FPM,静态文件自己直接返回并进行缓存优化,而那些既不是文件也不是目录的请求则通过
try_files
index.php
在配置Yii框架的Nginx时,有些坑是大家经常会踩的,同时也有不少方法可以提升性能。
常见错误:
root
root
web
protected
vendor
index.php
root
your_app/web
PHP-FPM未运行或配置不当:Nginx只是个HTTP服务器,它本身不执行PHP代码。它需要把PHP请求转发给PHP-FPM来处理。
location ~ \.php$
sudo systemctl status php-fpm
php7.4-fpm
fastcgi_pass
try_files
try_files
index.php
location / { try_files $uri $uri/ /index.php?$args; }文件权限问题:Nginx和PHP-FPM通常以低权限用户(如
www-data
nginx
runtime
web/assets
storage/web
runtime
web/assets
fastcgi_param SCRIPT_FILENAME
index.php
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
性能优化建议:
开启Gzip压缩:在Nginx配置中启用Gzip压缩可以显著减少传输数据量,加快页面加载速度。
gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; # 压缩级别,1-9,6是平衡点 gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
配置静态文件缓存:让浏览器缓存静态资源,减少重复请求。
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|eot|ttf|woff|woff2)$ {
expires 30d; # 缓存30天
add_header Cache-Control "public, must-revalidate";
access_log off; # 静态文件访问量大时可以关闭日志
}优化PHP-FPM配置:调整PHP-FPM的进程管理参数(如
pm.max_children
pm.start_servers
pm.min_spare_servers
pm.max_spare_servers
/etc/php/X.X/fpm/pool.d/www.conf
使用PHP Opcode Cache:开启PHP的Opcode缓存(如OPcache)可以避免每次请求都重新编译PHP脚本,显著提升PHP执行效率。
php.ini
opcache.enable=1
opcache.memory_consumption
Nginx FastCGI缓存:对于不经常变化的动态内容,Nginx可以缓存PHP-FPM的响应,直接返回缓存内容而无需再次请求PHP-FPM。
# 在http块中定义fastcgi_cache_path
# http {
# fastcgi_cache_path /var/cache/nginx/fastcgi_cache levels=1:2 keys_zone=my_cache:10m inactive=60m;
# fastcgi_cache_key "$scheme$request_method$host$request_uri";
# ...
# }
# 在server块或location块中启用
# location ~ \.php$ {
# fastcgi_cache my_cache;
# fastcgi_cache_valid 200 30m; # 缓存200状态码的响应30分钟
# fastcgi_cache_use_stale error timeout invalid_header http_500;
# add_header X-FastCGI-Cache $upstream_cache_status; # 方便调试缓存是否命中
# ...
# }这个需要根据实际业务逻辑谨慎使用,因为Yii应用通常是动态的,缓存不当可能导致显示旧数据。
限制不必要的日志记录:对于高流量的网站,过多的日志写入会带来I/O开销。对于静态文件可以关闭
access_log
合理设置client_max_body_size
client_max_body_size 20M; # 允许最大20MB的请求体
这个通常放在
http
server
通过细致地检查和调整这些配置,你的Yii应用在Nginx下会跑得更稳、更快。记住,每次修改Nginx配置后,都要
sudo nginx -t
sudo systemctl reload nginx
restart
以上就是YII框架的Nginx配置是什么?YII框架如何配置Nginx?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号