Nginx配置文件

Nginx配置文件

Nginx主配置文件/etc/nginx/nginx.conf是一個純文本類型的文件,整個配置文件是以區塊的形式組織的。通常,每一個區塊以一對大括號{}來表示開始與結束。php

1.Main位於nginx.conf配置文件的最高層
2.Main層下能夠有Event、HTTP層
3.HTTP層下面有容許有多個Server層, 用於對不一樣的網站作不一樣的配置
4.Server層也容許有多個Location, 用於對不一樣的路徑進行不一樣模塊的配置html

//nginx默認配置語法
worker_processes    //工做進程, 配置和CPU個數保持一致
error_log           //錯誤日誌, 後面接入的是路徑
pid                 //Nginx服務啓動時的pid

//events事件模塊
events {    //事件模塊            
    worker_connections  1024;   //每一個worker進程支持的最大鏈接數
    use                 //內核模型,select,poll,epoll
}

//非虛擬主機的配置或公共配置定義在http{}段內, server{}段外
http {
...    
    //必須使用虛擬機配置站點, 每一個虛擬機使用一個server{}段
    server {
        listen       80;    //監聽端口, 默認80
        server_name  localhost; //提供服務的域名或主機名
        
        //控制網站訪問路徑
        location / {
            root   /usr/share/nginx/html;   //存放網站路徑
            index  index.html index.htm;    //默認訪問首頁文件
        }
        //錯誤頁面,統必定義錯誤頁面
        //定義請求錯誤, 指定錯誤代碼
        
        則。
        error_page   500 502 503 504  /50x.html;
        //錯誤代碼重定向到新的Locaiton
        location = /50x.html {
            root   html;
        }
    }
    ...
    //第二個虛擬主機配置
    server {
    ...
    }
}

Nginx日誌配置

在學習日誌以前, 咱們須要先了解下HTTP請求和返回node

curl -v http://www.baidu.com

Nginx日誌配置規範nginx

//配置語法: 包括: error.log access.log
Syntax: log_format name [escape=default|json] string ...;
Default:    log_format combined "...";
Context:    http

//Nginx默認配置
log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                   '$status $body_bytes_sent "$http_referer" '
                  '"$http_user_agent" "$http_x_forwarded_for"';

$remote_addr    //表示客戶端地址
$remote_user    //http客戶端請求nginx認證用戶名
$time_local     //Nginx的時間
$request        //Request請求行, GET等方法、http協議版本
$status         //respoence返回狀態碼
$body_bytes_sent    //從服務端響應給客戶端body信息大小
$http_referer       //http上一級頁面, 防盜鏈、用戶行爲分析
$http_user_agent    //http頭部信息, 客戶端訪問設備
$http_x_forwarded_for   //http請求攜帶的http信息

Nginx狀態監控

--with-http_stub_status_module記錄Nginx客戶端基本訪問狀態信息web

Syntax: stub_status;
Default:    —
Context:    server, location

具體配置以下:shell

location /mystatus {
    stub_status on;
    access_log off;
}

//Nginx_status概述
Active connections:2    //Nginx當前活躍鏈接數
server accepts handled requests
16     16     19
server表示Nginx啓動到如今共處理了16個鏈接。
accepts表示Nginx啓動到如今共成功建立16次握手。
請求丟失數=(握手數-鏈接數)能夠看出,本次狀態顯示沒有丟失請求。
handled requests,表示總共處理了19次請求。
Reading     Nginx讀取到客戶端的 Header 信息數。
Writing     Nginx返回給客戶端的 Header 信息數。
Waiting    Nginx開啓keep-alive長鏈接狀況下, 既沒有讀也沒有寫, 創建鏈接狀況

Nginx下載站點

Nginx默認是不容許列出整個目錄瀏覽下載。json

Syntax: autoindex on | off;
Default:    
autoindex off;
Context:    http, server, location

//autoindex經常使用參數
autoindex_exact_size off;
默認爲on, 顯示出文件的確切大小,單位是bytes。
修改成off,顯示出文件的大概大小,單位是kB或者MB或者GB。

autoindex_localtime on;
默認爲off,顯示的文件時間爲GMT時間。
修改成on, 顯示的文件時間爲文件的服務器時間。

charset utf-8,gbk;
默認中文目錄亂碼,添加上解決亂碼。

配置目錄瀏覽功能vim

//開啓目錄瀏覽
location / {
    root html;
    autoindex on;
    autoindex_localtime on;
    autoindex_exact_size off;
}

Nginx訪問限制

鏈接頻率限制 limit_conn_module
請求頻率限制 limit_req_module服務器

http協議的鏈接與請求
HTTP是創建在TCP, 在完成HTTP請求須要先創建TCP三次握手(稱爲TCP鏈接),在鏈接的基礎上在HTTP請求。併發

HTTP請求創建在一次TCP鏈接基礎上
一次TCP請求至少產生一次HTTP請求

HTTP協議版本 鏈接關係
HTTP1.0 TCP不能複用
HTTP1.1 順序性TCP複用
HTTP2.0 多路複用TCP複用

Nginx鏈接限制配置:

//全局定義鏈接限制
Syntax:  limit_conn_zone key zone=name:size;
Default: —
Context: http
//引用鏈接限制
Syntax: limit_conn zone number;
Default: —
Context: http, server, location


//具體配置以下:
http {
//http段配置鏈接限制, 同一時刻只容許一個客戶端IP鏈接
limit_conn_zone $binary_remote_addr zone=conn_zone:10m;
    ...
    server {
    ...  

        location / {
        //同一時刻只容許一個客戶端IP鏈接
            limit_conn conn_zone 1;
        }


//壓力測試
yum install -y httpd-tools
ab -n 50 -c 20  http://127.0.0.1/index.html

Nginx請求限制配置:

//全局定義請求限制
Syntax:  limit_conn_zone key zone=name:size rate=rate;
Default: —
Context: http
//引用請求限制
Syntax: limit_conn zone number [burst=number] [nodelay];
Default: —
Context: http, server, location


//具體配置以下:
http {
//http段配置請求限制, rate限制速率,限制一秒鐘最多一個IP請求
limit_req_zone $binary_remote_addr zone=req_zone:10m rate=1r/s;
    ...
    server {
    ...  

        location / {
        //1r/s只接收一個請求,其他請求拒絕處理並返回錯誤碼給客戶端
            limit_req zone=req_zone;
        //請求超過1r/s,剩下的將被延遲處理,請求數超過burst定義的數量, 多餘的請求返回503
            #limit_req zone=req_zone burst=3 nodelay;
        }


//壓力測試
yum install -y httpd-tools
//五十次鏈接,併發二十
ab -n 50 -c 20  http://127.0.0.1/index.html

鏈接限制沒有請求限制有效?

咱們前面說過, 多個請求能夠創建在一次的TCP鏈接之上, 那麼咱們對請求的精度限制,固然比對一個鏈接的限制會更加的有效。

由於同一時刻只容許一個鏈接請求進入。
可是同一時刻多個請求能夠經過一個鏈接進入。
因此請求限制纔是比較優的解決方案。

Nginx訪問控制

基於IP的訪問控制 http_access_module
基於用戶登錄認證 http_auth_basic_module

基於IP的訪問控制

//容許配置語法
Syntax: allow address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except
//拒絕配置語法
Syntax: deny address | CIDR | unix: | all;
Default:    —
Context:    http, server, location, limit_except

//配置拒絕某一個IP, 其餘所有容許
location ~ ^/1.html {
    root /usr/share/nginx/html;
    index index.html;
    deny 192.168.56.1;
    allow all;
}

//只容許某一個網段訪問,其它所有拒絕
location / {
    root   html;
    index  index.php index.html index.htm;
    allow   192.168.56.0/24;
    deny    all;
}

http_access_module侷限性

下圖是使用http_x_forwarded_for記錄真實客戶端IP地址以及代理服務器IP

解決方式
1.採用HTTP頭信息控制訪問, 代理以及web服務開啓http_x_forwarded_for
2.結合geo模塊做
3.經過HTTP自動以變量傳遞

基於用戶登錄認證

//配置語法
Syntax: auth_basic string| off;
Default:    auth_basic off;
Context:    http, server, location, limit_except
//用戶密碼記錄配置文件
Syntax: auth_basic_user_file file;
Default:    -
Context:    http, server, location, limit_except


//須要安裝依賴組件
[root@xuliangwei ~]# yum install httpd-tools
[root@xuliangwei ~]# htpasswd -c /etc/nginx/auth_conf xuliangwei

//可在http,server,location下添加以下信息
auth_basic "Auth access Blog Input your Passwd!";
auth_basic_user_file /etc/nginx/auth_conf;

用戶認證侷限性
1.用戶信息依賴文件方式
2.操做管理機械,效率低下

解決辦法
1.Nginx結合LUA實現高效驗證
2.Nginx結合LDAP, 利用nginx-auth-ldap模塊

Nginx虛擬主機

所謂虛擬主機,在web服務器裏是一個獨立的網站站點,這個站點對應獨立的域名(也多是IP或端口),具備獨立的程序及資源目錄,能夠獨立地對外提供服務供用戶訪問。

配置基於域名虛擬主機

1.建立web站點目錄
[root@LNMP conf]# mkdir /soft/code/{www,bbs}
[root@LNMP conf]# echo "www" > /soft/code/www/index.html
[root@LNMP conf]# echo "bbs" > /soft/code/bbs/index.html

2.配置虛擬主機
[root@LNMP conf]# cat conf.d/{www,bbs}.conf
server {
    listen       80;
    server_name  www.xuliangwei.com;
    root /soft/code/www;
    ...
}
 
server {
    ...
    listen       80;
    server_name  bbs.xuliangwei.com;
    root /soft/code/bbs;
}

配置不一樣端口訪問不一樣虛擬主機

//僅修改listen監聽端口便可, 但不能和系統端口發生衝突
server {
    ...
    listen       8001;
    ...
}
 
server {
    ...
    listen       8002;
    ...
}

配置虛擬主機別名

所謂虛擬主機別名,就是虛擬主機設置除了主域名之外的一個域名,實現用戶訪問的多個域名對應同一個虛擬主機網站的功能。

以www.xuliangwei.com域名的虛擬主機爲例:
爲其增長一個別名xuliangwei.com時,出現網站內容和訪問www.xuliangwei.com是同樣的,具體配置以下:

//默認配置
[root@LNMP ~]# vim /etc/nginx/nginx.conf
server {
    listen       80;
    server_name www.xuliangwei.com;
}

//別名配置
[root@LNMP ~]# vim /etc/nginx/nginx.conf
server {
    listen       80;
    server_name  www.xuliangwei.com xuliangwei.com;
    ...
}

//使用Linux下curl測試結果
[root@LNMP conf]# curl xuliangwei.com
www.xuliangwei.com
[root@LNMP conf]# curl www.xuliangwei.com
www.xuliangwei.com

//訪問帶www和不帶www是同樣的, 除了別名實現也能夠經過rewrite實現
相關文章
相關標籤/搜索