Nginx 反向代理(四):实现 FastCGI 代理
|
admin
2025年6月29日 18:36
本文热度 29
|
4.7.1 相关指令和参数
关于 PHP 和 FastCGI 的内容请回顾 Apache 章节内容,此章节重点讲解 Nginx 将前端请求通过 FastCGI 协议反向代理到后端的 PHP-FPM,将请求交由 PHP 程序处理
http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html
fastcgi_index name;
fastcgi_pass address;
fastcgi_param parameter value [if_not_empty];
#安装php-fpm
[root@ubuntu ~]# apt install php8.1-fpm
#查看版本
[root@ubuntu ~]# php -v
PHP 8.1.2-1ubuntu2.14 (cli) (built: Aug 18 2023 11:41:11) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.1.2, Copyright (c) Zend Technologies
with Zend OPcache v8.1.2-1ubuntu2.14, Copyright (c), by Zend Technologies
#查看服务状态
[root@ubuntu ~]# systemctl status php8.1-fpm.service
#查看所有文件
[root@ubuntu ~]# dpkg -L php8.1-fpm
#查看配置文件
[root@ubuntu ~]# ls -lh /etc/php/8.1/
total 12K
drwxr-xr-x 3 root root 4.0K Feb 18 16:05 cli # cli 模式配置
drwxr-xr-x 4 root root 4.0K Feb 18 16:05 fpm # fpm 模式配置
drwxr-xr-x 2 root root 4.0K Feb 18 16:05 mods-available # cli 和 fpm 中引用的具体内容
[root@ubuntu ~]# ls /etc/php/8.1/fpm/
conf.d php-fpm.conf php.ini pool.d
[root@ubuntu ~]
pid = /run/php/php8.1-fpm.pid
error_log = /var/log/php8.1-fpm.log
[root@ubuntu ~]
user = www-data
group = www-data
listen = /run/php/php8.1-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3
[root@ubuntu ~]
listen = /run/php/php8.1-fpm.sock
[root@ubuntu ~]
total 4.0K
-rw-r--r-- 1 root root 3 Feb 18 22:44 php8.1-fpm.pid
srw-rw---- 1 www-data www-data 0 Feb 18 22:44 php8.1-fpm.sock
lrwxrwxrwx 1 root root 30 Feb 18 22:44 php-fpm.sock -> /etc/alternatives/php-fpm.sock
[root@ubuntu ~]
;listen = /run/php/php8.1-fpm.sock
listen = 127.0.0.1:9000
[root@ubuntu ~]
;pm.status_path = /status
pm.status_path = /php-status
pm.status_listen = 127.0.0.1:9001
ping.path = /ping
ping.response = pong
[root@ubuntu ~]
[root@ubuntu ~]
4.7.3 配置 Nginx 转发到 FastCGI[root@ubuntu ~]# ll /etc/nginx/fastcgi*
-rw-r--r-- 1 root root 1125 May 20 2025 /etc/nginx/fastcgi.conf
-rw-r--r-- 1 root root 1055 May 20 2025 /etc/nginx/fastcgi_params
[root@ubuntu ~]# wc -l /etc/nginx/fastcgi*
27 /etc/nginx/fastcgi.conf
26 /etc/nginx/fastcgi_params
[root@ubuntu ~]# diff /etc/nginx/fastcgi.conf /etc/nginx/fastcgi_params
2d1
< fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# /etc/nginx/fastcgi.conf 和 /etc/nginx/fastcgi_params 都是用于配置 FastCGI 的默认参数的文件
# 这两个文件中都用 fastcgi_param 指令定义了很多要传给后端 FastCGI 接口的参数
# fastcgi.conf 比 fastcgi_params 中多出一行配置,至于为什么要用两个文件,属于历史遗留问题
# 在设置 FastCGI 反向代理时可以引用这两个文件中的任意一个,但引用 fastcgi_params 时,要额外加一行
# fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
server {
listen 80;
server_name www.m99-josedu.com;
root /var/www/html/www.m99-josedu.com;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
location /php-status {
fastcgi_pass 127.0.0.1:9001;
include fastcgi.conf;
}
location = /ping {
fastcgi_pass 127.0.0.1:9000;
include fastcgi.conf;
}
location /status {
stub_status;
}
}
[root@ubuntu ~]
total 12K
-rw-r--r-- 1 root root 11 Feb 20 14:08 index.html
-rw-r--r-- 1 root root 17 Feb 20 14:09 index.php
-rw-r--r-- 1 root root 23 Feb 20 14:09 test.php
[root@ubuntu ~]
<?php
phpinfo();
[root@ubuntu ~]
<?php
echo "test.php";
http:
http:
http:
http:
http:
http:
http:
[root@ubuntu ~]
upstream group1 {
server 10.0.0.210;
server 10.0.0.159 down;
}
server {
listen 80;
server_name bbs.m99-josedu.com;
location / {
proxy_pass http://group1;
proxy_set_header host $http_host;
}
}
[root@ubuntu ~]
[root@ubuntu ~]
server {
listen 80;
server_name bbs.m99-josedu.com;
root /var/www/html/bbs.m99-josedu.com;
index index.html index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi.conf;
}
}
[root@ubuntu ~]
;listen = /run/php/php8.1-fpm.sock
listen = 127.0.0.1:9000
[root@ubuntu ~]
[root@ubuntu ~]
[root@ubuntu ~]
[root@ubuntu ~]
[root@ubuntu ~]
#DB 配置
[root@ubuntu ~]# apt update;apt install mysql-server-8.0 redis-server
#修改 mysql 配置
[root@ubuntu ~]# cat /etc/mysql/mysql.conf.d/mysqld.cnf
#注释两行
#bind-address = 127.0.0.1
#mysqlx-bind-address = 127.0.0.1
#添加两行
skip-name-resolve
default_authentication_plugin=mysql_native_password
[root@ubuntu ~]# systemctl restart mysql.service
#创建数据库,创建账号并授权
mysql> create database discuz;
mysql> create user 'discuzer'@'10.0.0.%' identified by '123456';
mysql> grant all on discuz.* to 'discuzer'@'10.0.0.%';
mysql> flush privileges;
# 通过浏览器完成安装,在浏览器中打开 http://bbs.m99-josedu.com
# 然后修改 Proxy Server 配置,再完成另外一台安装,都完成安装后再次修改配置,让两台 Web Server 都上线
# 测试OK,但如果上传附件,图片,会发现有时候会加载不出来,因为文件只上传到了其中一台 Web Server 上
— END —
阅读原文:原文链接
该文章在 2025/7/1 23:15:32 编辑过