Nginx配置常见难点概述
将Web服务器环境从Windows平台的Apache切换至Linux平台的Nginx后,开发者往往会遇到一系列配置陷阱。为了帮助后续使用者避免重复犯错,特此整理相关注意事项。
在实施Nginx配置过程中,通常涉及以下几个关键难点:
- 启用Nginx以支持pathinfo运行模式
- 优化URL结构,实现隐藏index.php入口文件
- 在同一台服务器上部署并管理多个独立站点
开启Pathinfo模式的关键配置
关于pathinfo模式的具体配置方法
需要在对应的server块(即单个站点配置)中添加如下代码片段:
location ~ ^(.+\.php)(.*)$ {
root html/[站点目录]; #配置站点目录路径
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
其中,root指令后的路径必须严格对应当前站点的实际根目录。若未正确设置,Nginx将回退至默认的html目录,导致路径解析错误。具体表现为:即便在location中定义了root,访问http://localhost仍可能跳转至/usr/local/nginx/html/index.php(源码安装路径)或/usr/share/nginx/html/index.php(yum安装路径)。建议注释掉原有的fastcgi默认配置以避免冲突。
使用Rewrite隐藏Index.php入口
针对隐藏index.php的需求
多数情况下,隐藏index.php依赖于Nginx的重写机制。以下提供一份经过验证的参考配置方案:
location / {
root html/[站点目录];
index index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php/$1;
}
}
上述配置中的站点目录应指向包含index.php文件的程序根目录。例如,在使用CI框架时,root应设置为html/ci,因为该框架的入口文件位于html/ci/index.php。
本质上,隐藏index.php即是URL重写的一种应用。如需深入了解底层逻辑,可查阅Nginx官方文档中的Module ngx_http_rewrite_module章节。
单服务器多站点部署方案
同一服务器上的多站点配置策略
生产环境中,单台服务器通常承载多个网站服务,而非仅运行单一站点。
Nginx配置多站点操作简便且高效。其核心逻辑在于一个server块对应一个独立站点。示例如下:
server {
listen 80;
server_name www.leslie.net.cn;
location / {
....
}
}
server {
listen 80;
server_name www.hellomyfrend.top;
location / {
....
}
}
通过上述配置,即可成功建立两个独立站点,分别对应域名www.leslie.net.cn和www.hellomyfrend.top。
此处提供一份完整的配置文件供读者参考借鉴:
user nginx nginx;
worker_processes 2;
#error_log logs/error.log;
error_log logs/error.log notice;
#error_log logs/error.log info;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
rewrite_log on;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name pay.zf2.com;
location / {
root html/zf2/pay/public;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1;
}
}
location ~ ^(.+\.php)(.*)$ {
root html/zf2/pay/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}
server {
listen 80;
server_name mp.zf2.com;
location / {
root html/zf2/server/public;
index index.php index.html index.htm;
if (!-e $request_filename){
rewrite ^(.*)$ /index.php/$1;
}
}
location ~ ^(.+\.php)(.*)$ {
root html/zf2/server/public;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
}
}
以上内容涵盖了Nginx配置中的主要痛点,希望能对读者的学习与实践有所帮助,同时也欢迎继续支持本站获取更多技术分享。