nginx之动静分离(nginx与php不在同一台服务器)

nginx实现动静分离(nginx与php不在同一个服务器)

使用wordpress-5.0.3-zh_CN.tar.gz做实验

Nginx服务器的配置:
[root@app ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz
[root@app ~]# mv wordpress/* /data/nginx/php/
[root@app ~]# vim /apps/nginx/conf/conf.d/pc.conf
server {
   listen 80;
   server_name www.xxxpc.net;
   location / {
      root /data/nginx/php;       #指定静态资源的根,wordpress中的html文件和目录没有指定静态文件的具体位置,所以默认会去nginx的/下寻找静态资源
      index index.html;
   }
   location ~ \.php$ {
      root /data/nginx/php;       #指定php服务器存放资源文件的根路径
      fastcgi_pass 192.168.38.37:9000;    #发现php后缀的资源,反向代理给指定IP和端口号的php服务器
      fastcgi_index index.php;    #php的默认站点页面
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  #告知php服务器用户请求的资源的绝对路径和资源名称
      include fastcgi_params;     #包含fastcgi_params文件中的所有参数
   }
}

php服务器配置:
[root@localhost ~]# tar xf wordpress-5.0.3-zh_CN.tar.gz 
[root@localhost ~]# mv wordpress/* /data/nginx/php/     #把wordpress移动到Nginx所指定的php服务器的动态资源路径下
[root@localhost ~]# cd /data/nginx/php/
[root@localhost php]# mysql
MariaDB [(none)]> grant all on wordpress.* to wordpress@'192.168.38.%' identified by 'centos';   #创建php连接数据库的用户和密码
MariaDB [(none)]> create database wordpress;   #创建wordpress用于存放数据的数据库
[root@localhost php]# cp wp-config-sample.php wp-config.php   #复制php连接数据库的配置文件
[root@localhost php]# vim wp-config.php    #修改一下php连接数据库的各种配置即可
[root@localhost php]# systemctl start php72-php-fpm

注:当Nginx和php不在同一台服务器时,Nginx和php各自服务器上都需要有wordpress的各种资源文件;因为用户请求的资源,只有动态资源才会往php服务器上转发,静态资源Nginx自己进行回复;所以如果php有wordpress资源,但是Nginx上没有,则会造成页面无渲染效果;反之,如果Nginx上有wordpress资源,php上没有,则会提示File not found.

猜你喜欢

转载自www.cnblogs.com/dongzhanyi123/p/12081461.html