国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 網(wǎng)站 > Nginx > 正文

詳解Nginx配置多站點(diǎn)需要踩的坑

2024-08-30 12:22:06
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
本篇文章主要介紹了詳解Nginx配置多站點(diǎn)需要踩的坑,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

從Windows下的Apache轉(zhuǎn)移到Linux下的Nginx,發(fā)現(xiàn)有很多坑需要踩。
以下就做個(gè)簡(jiǎn)單的記錄,方便后來(lái)者爬坑。

配置Nginx,一般會(huì)遇到以下幾個(gè)坑:

 配置nginx支持pathinfo模式  - 優(yōu)化URL,隱藏index.php  - 同一服務(wù)器配置多站點(diǎn) 

配置pathinfo模式

在server(也就是你的站點(diǎn),一個(gè)server對(duì)應(yīng)一個(gè)站點(diǎn))中輸入以下內(nèi)容:

location ~ ^(.+/.php)(.*)$ { root html/[站點(diǎn)目錄](méi); #配置站點(diǎn)目錄路徑 fastcgi_split_path_info ^(.+/.php)(.*)$; fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_script_name;}

其中root html/[站點(diǎn)目錄](méi)這個(gè)一定要填寫(xiě)跟你server的站點(diǎn)目錄路徑,要不它默認(rèn)為html,從而導(dǎo)致路徑解析失敗。會(huì)出現(xiàn)的現(xiàn)象就是,在之前配置的location中定義了root路徑也無(wú)法生效,訪(fǎng)問(wèn)http://localhost會(huì)跳轉(zhuǎn)到/usr/local/nginx/html/index.php[index.html],因?yàn)槲业膎ginx是源碼安裝,所以路徑可能不同,yum安裝的話(huà)一般會(huì)在/usr/share/nginx/html/index.php[index.html].(PS:原默認(rèn)關(guān)于fastcgi的配置可以注釋掉)

隱藏index.php

隱藏index.php,大多數(shù)是采用Nginx的重寫(xiě)規(guī)則來(lái)進(jìn)行的。
下面,就是博主的列出的一個(gè)參考:

location / { root html/[站點(diǎn)目錄](méi); index index.php; if (!-e $request_filename) {  rewrite ^(.*)$ /index.php/$1; }}

這里的站點(diǎn)目錄是你程序(框架)的index.php所在的目錄。例如,CI框架的話(huà),root html/ci,其中index.php位于html/ci/index.php.

本質(zhì)上,隱藏index.php文件就是重寫(xiě)URL。具體詳細(xì)用法可以參考Nginx重寫(xiě)模塊的官方文檔:Module ngx_http_rewrite_module

同一服務(wù)器多站點(diǎn)配置

一般一臺(tái)服務(wù)器不會(huì)單一的運(yùn)行一個(gè)站點(diǎn),往往是運(yùn)行多個(gè)站點(diǎn)的。

在Nginx配置多站點(diǎn)是非常簡(jiǎn)單,便捷的。正如,前面所說(shuō)的,一個(gè)server對(duì)應(yīng)一個(gè)站點(diǎn)。例如:

server { listen 80; server_name www.leslie.net.cn; location / { .... }}server { listen 80; server_name www.hellomyfrend.top; location / { .... }}

這樣,就配置了兩個(gè)站點(diǎn),分別為www.leslie.net.cn和www.hellomyfrend.top.

這里貼一份配置文件作為參考:

user nginx nginx;worker_processes 2;#error_log logs/error.log;error_log logs/error.log notice;#error_log logs/error.log info;pid  logs/nginx.pid;events { worker_connections 1024;} http { include  mime.types; default_type application/octet-stream;#log_format main '$remote_addr - $remote_user [$time_local] "$request" '#     '$status $body_bytes_sent "$http_referer" '#     '"$http_user_agent" "$http_x_forwarded_for"';#access_log logs/access.log main; rewrite_log on; sendfile  on;#tcp_nopush  on;#keepalive_timeout 0; keepalive_timeout 65;#gzip on; server {  listen 80;  server_name  pay.zf2.com;  location / {   root html/zf2/pay/public;   index index.php index.html index.htm;    if (!-e $request_filename){    rewrite ^/(.*)$ /index.php/$1;   }  }  location ~ ^(.+/.php)(.*)$ {   root html/zf2/pay/public;   fastcgi_split_path_info ^(.+/.php)(.*)$;   fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;    fastcgi_index index.php;   include fastcgi_params;   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;   fastcgi_param PATH_INFO $fastcgi_script_name;  } } server {  listen 80;  server_name mp.zf2.com;  location / {   root html/zf2/server/public;   index index.php index.html index.htm;   if (!-e $request_filename){   rewrite ^(.*)$ /index.php/$1;   }  }  location ~ ^(.+/.php)(.*)$ {   root html/zf2/server/public;   fastcgi_split_path_info ^(.+/.php)(.*)$;   fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock;    fastcgi_index index.php;   include fastcgi_params;   fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;   fastcgi_param PATH_INFO $fastcgi_script_name;  } }}
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 顺昌县| 修水县| 五台县| 东宁县| 扶绥县| 梨树县| 昂仁县| 玉环县| 咸宁市| 枣庄市| 遂昌县| 会泽县| 安化县| 北辰区| 错那县| 阿拉善左旗| 桂平市| 五原县| 晋江市| 张家界市| 宝鸡市| 大同县| 奉化市| 永康市| 上杭县| 洪洞县| 偏关县| 个旧市| 威远县| 大关县| 瑞金市| 万山特区| 西林县| 清苑县| 来宾市| 临潭县| 五常市| 德惠市| 轮台县| 高尔夫| 合作市|