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.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM