Nginx可以实现动态和静态分离的操作,一般情况下用于Nginx分离PHP或者Nginx分离js脚本,通过请求的类型转发请求到应用服务器;
本篇文章讲解使用Nginx配置PHP分离文件。
PHP FastCGI配置分离的Nginx配置文件
PHP服务器IP:10.0.0.99:9000
Nginx服务器IP:10.0.0.113
测试网站使用之前配置的blog服务器
location ~ ^/.+\.php {
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
fastcgi_pass 10.0.0.99:9000;
}
PHP独立服务器安装PHP7.3
#安装运行环境
yum -y install gcc gcc-c++
#安装第三方软件源
yum -y install epel-release
#安装软件包
yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
#安装包管理器组件
yum -y install yum-utils
#启用php7.3源
yum-config-manager --enable remi-php73
#安装PHP7需要的组件
yum -y install php php-mcrypt php-devel php-cli php-gd php-pear php-curl php-fpm php-mysql php-ldap php-zip php-fileinfo
#安装完了查看版本
php -v
#安装版本信息
PHP 7.3.33 (cli) (built: Dec 19 2022 14:30:27) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies
#启动php-fpm,开机自启动
systemctl start php-fpm
systemctl enable php-fpm
#防火墙放行端口9000 TCP
firewall-cmd --add-port=9000/tcp --zone=public --permanent
firewall-cmd --reload
#可以关闭防火墙,在实际生产环境中间请勿随意关闭防火墙
#关闭SELINUX,不关闭所有服务只有OpenSSH服务是可以被访问的,其他的服务都无法通过网络访问
setenforce 0
#临时关闭SELINUX
#安装mbstring
yum -y install php-mbstring
#在php.ini设置mbstring弃用
nano /etc/php.ini
#在文件内加入这一行
extension=mbstring.so
#保存重启PHP-FPM
systemctl restart php-fpm
修改Nginx的配置文件
upstream php-fastcgi {
server 127.0.0.1:9000;
server 10.0.0.113:9000;
}
server {
listen 80;
listen 443 ssl;
server_name blog.nginx.test;
alias /www/blog.nginx.test;
index index.html index.htm index.php;
ssl_certificate /cert/local.crt;
ssl_certificate_key /cert/local.key;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!CAMELLIA:!DES:!MD5:!PSK:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_session_cache shared:SSL:5m;
ssl_session_timeout 5m;
if ($scheme = http) {
return 301 https://$host$request_uri;
}
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
location ~ ^/.+\.php {
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(/.*)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
fastcgi_pass php-fastcgi;
}
#设置缓存3天
location ~* \.(js|css|jpg|gif|png|bmp|swf)$ {
expires 3d;
}
}
重启Nginx服务后打开浏览器查看网站能不能正常打开
systemctl restart nginx
浏览器打开网站
评论