以下是nginx的默認配置文件: server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ /.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ /.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ //.ht { # deny all; #} }
一個location匹配一個(一種)url。
匹配到對應的url就轉到對應的location中,然后進行處理請求。
默認的root是/usr/share/nginx/html,也就是會轉到對應的目錄下進行處理請求。
但是匹配到有php的文件就沒有辦法進行解析。
這里用到了php-fpm 也就是fastcgi在php下的一個類庫。
當匹配到有.php結尾的請求的時候,就將該請求轉到fastcgi進行處理。
關于server_name 還糾結了好久,當我在同一操作系統下有兩個用戶a和b,兩者都有各自的root目錄進行訪問,并且同時都配置了各自的server。但是nginx的配置文件是在全局其作用的。那么如果在局域網中c訪問這個服務器的時候,那么到底訪問哪個目錄呢?
后來,哥哥告訴我了。
一般在上線的網站中配置nginx的時候,server_name是域名,根據不同的域名,nginx自然是知道怎么去轉的。
如果是在局域網中用ip進行訪問,如果存在著兩個server,那么就配置一個當用ip進行訪問的時候,默認跳轉到一個目錄下就可以了。
自己在線下進行折騰的時候,就配置一個人server就可以了。
默認就會請求這個目錄,并用fastccgi進行轉發。
在配置nginx的時候發現了一個問題,訪問的root目錄下,.html結尾的靜態文件是可以正常被訪問的,.php結尾的就不行。
后來才發現,在進行配置的時候,將root變成了局部變量,導致匹配php文件的時候,找不到root。自然就找不到要訪問的文件了。
最后的配置如下:
鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答
圖片精選