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

首頁 > 網站 > Nginx > 正文

詳解Nginx配置多站點需要踩的坑

2024-08-30 12:28:32
字體:
來源:轉載
供稿:網友

從Windows下的Apache轉移到Linux下的Nginx,發現有很多坑需要踩。
以下就做個簡單的記錄,方便后來者爬坑。

配置Nginx,一般會遇到以下幾個坑:

  •  配置nginx支持pathinfo模式 
  • - 優化URL,隱藏index.php 
  • - 同一服務器配置多站點 

配置pathinfo模式

在server(也就是你的站點,一個server對應一個站點)中輸入以下內容:

location ~ ^(.+/.php)(.*)$ { root html/[站點目錄]; #配置站點目錄路徑 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/[站點目錄]這個一定要填寫跟你server的站點目錄路徑,要不它默認為html,從而導致路徑解析失敗。會出現的現象就是,在之前配置的location中定義了root路徑也無法生效,訪問http://localhost會跳轉到/usr/local/nginx/html/index.php[index.html],因為我的nginx是源碼安裝,所以路徑可能不同,yum安裝的話一般會在/usr/share/nginx/html/index.php[index.html].(PS:原默認關于fastcgi的配置可以注釋掉)

隱藏index.php

隱藏index.php,大多數是采用Nginx的重寫規則來進行的。
下面,就是博主的列出的一個參考:

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

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

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

同一服務器多站點配置

一般一臺服務器不會單一的運行一個站點,往往是運行多個站點的。

在Nginx配置多站點是非常簡單,便捷的。正如,前面所說的,一個server對應一個站點。例如:

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

這樣,就配置了兩個站點,分別為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;  } }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 庆安县| 博客| 河池市| 綦江县| 凤山市| 玛沁县| 安国市| 宁津县| 遂平县| 张家港市| 金昌市| 吉首市| 林口县| 永寿县| 辽阳县| 龙井市| 喜德县| 临西县| 曲周县| 奉贤区| 汪清县| 图片| 江口县| 靖江市| 平谷区| 美姑县| 夹江县| 雷波县| 大竹县| 常德市| 皮山县| 牙克石市| 龙里县| 和龙市| 新沂市| 赤城县| 天峨县| 丰原市| 蒙阴县| 合水县| 安泽县|