1、原由
只有一個域名,以前用php編寫的網(wǎng)頁又不能放棄,考慮將其移至二級目錄下,例如:
https://abc.com/html
2、運行環(huán)境
Linux服務器上,用docker容器。Nginx和php-fpm各自運行在不同的容器中,Nginx在前端負責接收http請求,將其分發(fā)到后面不同的服務容器中。收到PHP網(wǎng)頁請求就交給php-fpm解析,php-fpm的端口是9000。
3、Nginx配置
3.1 如果使用根目錄
如果php網(wǎng)頁在根目錄下就比較簡單,用下面配置即可:
server {
listen 80;
server_name abc.com;
root /usr/share/nginx/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php(.*)$ {
root /var/www/html/phpmy;
fastcgi_pass 172.17.0.1:9000; #php容器的IP地址
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;
}
}
}
3.2 將php網(wǎng)頁放在二級目錄
將3.1的方法直接移植過來是行不通的,先放結果,正確的配置文件如下:
location /html {
alias /var/www/html/; #PHP文件在php-fpm容器中的目錄
index index.php index.htm index.html;
try_files $uri $uri/ /html/index.php?$query_string;
if (!-e $request_filename) {
rewrite ^/html/(.*)$ /html/index.php?$1 last;
break;
}
location ~ \.php(.*)$ {
fastcgi_pass 172.17.0.1:9000; # PHP容器的IP地址
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $request_filename;
include fastcgi_params;
}
location ~* \.(htm|html|jpg|jpeg|png|gif|ico|js|css|map)$ {
root /usr/share/nginx/; # 靜態(tài)文件目錄比真實目錄少一級,訪問時會自動加上url的子目錄
}
}
與在根目錄下的情況不同,傳給 location /html的目錄是PHP文件在php-fpm容器中的目錄。php-fpm接收的SCRIPT_FILENAME是 $request_filename文章來源:http://www.zghlxwxcb.cn/news/detail-615095.html
- 下面一段是解決頁面跳轉后不能正確定位問題:
if (!-e $request_filename) {
rewrite ^/html/(.*)$ /html/index.php?$1 last;
break;
}
- 這個location是解決非php文件不能訪問的問題:
location ~* \.(htm|html|jpg|jpeg|png|gif|ico|js|css|map)$ {
root /usr/share/nginx/; # 靜態(tài)文件目錄比真實目錄少一級,訪問時會自動加上url的子目錄
}
目前這個配置文件還是有一些問題,比如直接訪問目錄時不能直接跳到index.php文件,訪問時必須這樣:文章來源地址http://www.zghlxwxcb.cn/news/detail-615095.html
https://abc.com/html/index.php
到了這里,關于好大一個坑:在Nginx上將PHP網(wǎng)頁放在二級目錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!