1 、顯示亂碼問題
server {
listen 80;
server_name example.com;
root /var/www/example;
location / {
charset utf-8; #一般是在個別的location中加入此項,具體情況具體對待
rewrite .* /index.html break;
}
}
2、index顯示列表(一般爲企業內部使用)
可在在location server 或 http段中加入
autoindex on;//自動顯示目錄
autoindex_exact_size off;//人性化方式顯示文件大小否則以byte顯示
autoindex_localtime on;//按服務器時間顯示,否則以gmt時間顯示
3、location用法
=:對URI做精確匹配;
location = / {
...
}
~:對URI做正則表達式模式匹配,區分字符大小寫;
~*:對URI做正則表達式模式匹配,不區分字符大小寫;
^~:對URI的左半部分做匹配檢查,不區分字符大小寫;
不帶符號:匹配起始於此uri的所有的url;
location /lizi {
...
}
例如www.example.com/lizi/xxx/xxx
只要是路徑以/lizi開頭的都匹配,這種一般用於最後的通用匹配。
注意:匹配優先級:=, ^~, ~/~*,不帶符號;
具體匹配方式http://seanlook.com/2015/05/17/nginx-location-rewrite/
3、常用正則(跟Linux上的正則沒什麼區別)
. : 匹配除換行符以外的任意字符
? : 重複0次或1次
+ : 重複1次或更多次
* : 重複0次或更多次
\d :匹配數字
^ : 匹配字符串的開始
$ : 匹配字符串的結束
{n} : 重複n次
{n,} : 重複n次或更多次
[c] : 匹配單個字符c
[a-z] : 匹配a-z小寫字母的任意一個
類似分組():==小括號()之間匹配的內容,可以在後面通過2表示的是前面第二個()裏的內容。正則裏面容易讓人困惑的是\轉義特殊字符。==
4、rewrite實例
http {
# 定義image日誌格式
log_format imagelog '[$time_local] ' $image_file ' ' $image_type ' ' $body_bytes_sent ' ' $status;
# 開啓重寫日誌
rewrite_log on;
server {
root /home/www;
location / {
# 重寫規則信息
error_log logs/rewrite.log notice;
# 注意這裏要用‘’單引號引起來,避免{}
rewrite '^/images/([a-z]{2})/([a-z0-9]{5})/(.*)\.(png|jpg|gif)$' /data?file=$3.$4;
# 注意不能在上面這條規則後面加上“last”參數,否則下面的set指令不會執行
set $image_file $3;
set $image_type $4;
}
location /data {
# 指定針對圖片的日誌格式,來分析圖片類型和大小
access_log logs/images.log mian;
root /data/images;
# 應用前面定義的變量。判斷首先文件在不在,不在再判斷目錄在不在,如果還不在就跳轉到最後一個url裏
try_files /$arg_file /image404.html;
}
location = /image404.html {
# 圖片不存在返回特定的信息
return 404 "image not found\n";
}
}
if語句塊
- 例子是網上找的,if語句塊長用在做單獨的限制,如限制訪問特定的資源,然後對此類請求做處理,rewire或者deny或者proxy_pass等等。
- 例子中的set語句沒怎麼用過。也沒再去查資料,用的時候再查 = =
if ($http_user_agent ~ MSIE) {proxy_pass
rewrite ^(.*)$ /msie/$1 break;
} //如果UA包含"MSIE",rewrite請求到/msid/目錄下
if ($http_cookie ~* "id=([^;]+)(?:;|$)") {
set $id $1;
} //如果cookie匹配正則,設置變量$id等於正則引用部分
if ($request_method = POST) {
return 405;
} //如果提交方法爲POST,則返回狀態405(Method not allowed)。return不能返回301,302
if ($slow) {
limit_rate 10k;
} //限速,$slow可以通過 set 指令設置
if (!-f $request_filename){
break;
proxy_pass http://127.0.0.1;
} //如果請求的文件名不存在,則反向代理到localhost 。這裏的break也是停止rewrite檢查
if ($args ~ post=140){
rewrite ^ http://example.com/ permanent;
} //如果query string中包含"post=140",永久重定向到example.com
https
- 定義一個新的server,配置如下,必須的配置有listen ,server_name, ssl ,ssl_certificate, ssl_certificate_key,一般配置的時候我都是直接複製,然後改主機名,證書私鑰文件,日誌路徑,root的根目錄這幾項。
- 如果想讓訪問80的轉到443,可用rewrite語句
listen 443;
server_name agent.t.jlhcar.com;
ssl on;
ssl_certificate, "/usr/local/certificate/xxxx.pem";\\證書
ssl_certificate_key "/usr/local/certificate/xxxx.key";\\私鑰
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_prefer_server_ciphers on;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;\\協議
...
//日誌以及root根目錄的其他配置
server {
listen 80;
server_name www.example.com
rewrite ^/(.*)$ https://www.example.com/$1;
}
php後端處理(fcgi)
location ~ \.php($|/) {
fastcgi_pass unix:/dev/shm/php-fpm.unix; //最重要的一項,根據實際情況來配置(根據php的配置文件listen的配置來配置)
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
負載均衡和反向代理
- 在http配置段要配置一個upstream
upstream ucart{
server host_ip:80;
server host_ip:80;
}
注意:nginx中不識別_(下劃線),否則會出現400錯誤
- 然後再server配置段,將所需要處理的請求反向代理至後端服務器,在可根據需要和服務器配置情況來定義權重,實現負載均衡
location /ucarapi/ {
proxy_pass http://ucar_t/;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_set_header Host www.example.com; //頭信息,後端服務器根據此來找到特定虛擬主機
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
}
喜歡我寫的東西的朋友可以關注一下我的公衆號,上面有我的學習資源以及一些其他福利。:Devops部落