Nginx与Apache共存背景
本实例详细解析了Linux环境下Nginx与Apache共存的配置方案,旨在为相关运维工作提供参考。
鉴于操作系统限制,单一端口不可被多个进程同时占用。因此,解决策略调整为在同一台服务器上,部分站点由Nginx处理,其余站点交由Apache托管,以实现混合部署。
核心解决思路如下:
部署Nginx兼具反向代理与Web服务器功能,使其负责监听标准的80端口;而Apache则配置监听非80端口,例如本例中使用的8080端口。

具体实施步骤如下:
- 在Linux环境中完成基础搭建后,依次安装Nginx和Apache服务,两者默认均绑定80端口。
- 常规客户端访问默认指向80端口,故设定Nginx作为静态页面服务端口为80;修改Apache配置文件httpd.conf中的Listen指令,将其端口调整为8080。
对于需运行在Apache下的网站:
需在Nginx的主配置文件nginx.conf中添加相应配置块。
server {
listen 80;
server_name www.one.ityangs.cn one.ityangs.cn;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
同时在Apache的配置文件httpd.conf中进行对应设置。
ServerName www.one.ityangs.cn
ServerAlias www.one.ityangs.cn one.ityangs.cn
DocumentRoot /www/one
DirectoryIndex index.php index.html
Options +Includes +FollowSymLinks -Indexes
AllowOverride All
Order Deny,Allow
Allow from All
对于需直接运行在Nginx下的网站:
同样需要在nginx.conf文件中补充相应的Server块配置。
server {
listen 80;
server_name two.ityangs.cn www.two.ityangs.cn;
root /www/two;
location /{
index index.html index.htm index.php;
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php?s=$1 last;
break;
}
error_page 404 /var/www/html/404.html;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
期望上述内容能为读者在Linux服务器维护过程中提供实用帮助。