Nginx 一點一滴 02 - 基礎配置

Nginx服務器基礎配置指令

nginx.conf文件的結構

  • Global: nginx運行相關
  • events: 與用戶的網絡鏈接相關
  • http

    • http Global: 代理,緩存,日誌,以及第三方模塊的配置
    • server

      • server Global: 虛擬主機相關
      • location: 地址定向,數據緩存,應答控制,以及第三方模塊的配置

全部的全部的全部的指令,都要以;結尾html

配置運行Nginx服務器用戶(組)

user nobody nobody;node

配置容許生成的worker process數

worker_processes auto;
worker_processes 4;linux

這個數字,跟電腦CPU核數要保持一致nginx

ganiks ➜  Nginx git:(master) ✗ grep ^proces /proc/cpuinfo
processor       : 0
processor       : 1
processor       : 2
processor       : 3
ganiks ➜  Nginx git:(master) ✗ grep ^proces /proc/cpuinfo | wc -l
4

配置Nginx進程PID存放路徑

pid logs/nginx.pid;git

這裏面保存的就是一個數字,nginx master 進程的進程號web

配置錯誤日誌的存放路徑

error_log logs/error.log;
error_log logs/error.log error;正則表達式

配置文件的引入

include mime.types;
include fastcgi_params;
include ../../conf/*.conf;apache

設置網絡鏈接的序列化

accept_mutex on;瀏覽器

對多個nginx進程接收鏈接進行序列化,防止多個進程對鏈接的爭搶(驚羣)緩存

設置是否容許同時接收多個網絡鏈接

multi_accept off;

事件驅動模型的選擇

use select|poll|kqueue|epoll|rtsig|/dev/poll|eventport

這個重點,後面再看

配置最大鏈接數

worker_connections 512;

定義MIME-Type

include mime.types;
default_type application/octet-stream;

自定義服務日誌

access_log logs/access.log main;
access_log off;

配置容許sendfile方式傳輸文件

sendfile off;

sendfile on;
sendfile_max_chunk 128k;

nginx 每一個worker process 每次調用 sendfile()傳輸的數據量的最大值

Refer:

配置鏈接超時時間

與用戶創建鏈接後,Nginx能夠保持這些鏈接一段時間, 默認 75s
下面的65s能夠被Mozilla/Konqueror識別,是發給用戶端的頭部信息Keep-Alive

keepalive_timeout 75s 65s;

單鏈接請求數上限

和用戶端創建鏈接後,用戶經過此鏈接發送請求;這條指令用於設置請求的上限數

keepalive_requests 100;

配置網絡監聽

listen *:80 | *:8000; # 監聽全部的80和8000端口

listen 192.168.1.10:8000;
listen 192.168.1.10;
listen 8000; # 等同於 listen *:8000;
listen 192.168.1.10 default_server backlog=511; # 該ip的鏈接請求默認由此虛擬主機處理;最多容許1024個網絡鏈接同時處於掛起狀態

基於名稱的虛擬主機配置

server_name myserver.com www.myserver.com;

server_name .myserver.com www.myserver. myserver2.*; # 使用通配符

不容許的狀況: server_name www.ab*d.com; # *只容許出如今www和com的位置

server_name ~^www\d+.myserver.com$; # 使用正則

nginx的配置中,能夠用正則的地方,都以~開頭

from Nginx~0.7.40 開始,server_name 中的正則支持 字符串捕獲功能(capture)

server_name ~^www.(.+).com$; # 當請求經過www.myserver.com請求時, myserver就被記錄到$1中, 在本server的上下文中就可使用

若是一個名稱 被多個虛擬主機的 server_name 匹配成功, 那這個請求到底交給誰處理呢?看優先級:

  1. 準確匹配到server_name
  2. 通配符在開始時匹配到server_name
  3. 通配符在結尾時匹配到server_name
  4. 正則表達式匹配server_name
  5. 先到先得

基於IP的虛擬主機配置

基於IP的虛擬主機,須要將網卡設置爲同時可以監聽多個IP地址

ifconfig
# 查看到本機IP地址爲 192.168.1.30
ifconfig eth1:0 192.168.1.31 netmask 255.255.255.0 up
ifconfig eth1:1 192.168.1.32 netmask 255.255.255.0 up
ifconfig
# 這時就看到eth1增長來2個別名, eth1:0 eth1:1

# 若是須要機器重啓後仍保持這兩個虛擬的IP
echo "ifconfig eth1:0 192.168.1.31 netmask 255.255.255.0 up" >> /etc/rc.local
echo "ifconfig eth1:0 192.168.1.32 netmask 255.255.255.0 up" >> /etc/rc.local

再來配置基於IP的虛擬主機

http {
    ...
    server {
     listen 80;
     server_name 192.168.1.31;
     ...
    }
    server {
     listen 80;
     server_name 192.168.1.32;
     ...
    }
}

配置location塊(重中之重)

location 塊的配置,應該是最經常使用的了

location [ = | ~ | ~* | ^~ ] uri {...}

這裏內容分2塊,匹配方式和uri, 其中uri又分爲 標準uri和正則uri

先不考慮 那4種匹配方式

  1. Nginx首先會再server塊的多個location中搜索是否有標準uri和請求字符串匹配, 若是有,記錄匹配度最高的一個;
  2. 而後,再用location塊中的正則uri和請求字符串匹配, 當第一個正則uri匹配成功,即中止搜索, 並使用該location塊處理請求;
  3. 若是,全部的正則uri都匹配失敗,就使用剛記錄下的匹配度最高的一個標準uri處理請求
  4. 若是都失敗了,那就失敗嘍

再看4種匹配方式:

  • =: 用於標準uri前,要求請求字符串與其嚴格匹配,成功則當即處理
  • ^~: 用於標準uri前,並要求一旦匹配到,當即處理,再也不去匹配其餘的那些個正則uri
  • ~: 用於正則uri前,表示uri包含正則表達式, 並區分大小寫
  • ~*: 用於正則uri前, 表示uri包含正則表達式, 不區分大小寫

^~ 也是支持瀏覽器編碼過的URI的匹配的哦, 如 /html/%20/data 能夠成功匹配 /html/ /data

配置請求的根目錄

Web服務器收到請求後,首先要在服務端指定的目錄中尋找請求資源

root /var/www;

更改location的URI

除了使用root指明處理請求的根目錄,還可使用alias 改變location收到的URI的請求路徑

location ~ ^/data/(.+\.(htm|html))$ {
    alias /locatinotest1/other/$1;
}

設置網站的默認首頁

index 指令主要有2個做用:

  • 對請求地址沒有指明首頁的,指定默認首頁
  • 對一個請求,根據請求內容而設置不一樣的首頁,以下:
location ~ ^/data/(.+)/web/$ {
    index index.$1.html index.htm;
}

設置網站的錯誤頁面

error_page 404 /404.html;
error_page 403 /forbidden.html;
error_page 404 =301 /404.html;

location /404.html {
    root /myserver/errorpages/;
}

基於IP配置Nginx的訪問權限

location / {
    deny 192.168.1.1;
    allow 192.168.1.0/24;
    allow 192.168.1.2/24;
    deny all;
}

從192.168.1.0的用戶時能夠訪問的,由於解析到allow那一行以後就中止解析了

基於密碼配置Nginx的訪問權限

auth_basic "please login";
auth_basic_user_file /etc/nginx/conf/pass_file;

這裏的file 必須使用絕對路徑,使用相對路徑無效

# /usr/local/apache2/bin/htpasswd -c -d pass_file user_name
# 回車輸入密碼,-c 表示生成文件,-d 是以 crypt 加密。

name1:password1
name2:password2:comment

通過basic auth認證以後沒有過時時間,直到該頁面關閉;
若是須要更多的控制,可使用 HttpAuthDigestModule http://wiki.nginx.org/HttpAuthDigestModule

Nginx服務器基礎配置實例

user ganiks ganiks;

worker_processes 3;

error_log logs/error.log;
pid myweb/nginx.pid;

events {
    use epoll;
    worker_connections 1024;
}

http {
    include mime.types;
    default_type applicatioin/octet-stream;

    sendfile on;

    keepalive_timeout 65;

    log_format access.log '$remote_addr [$time_local] "$request" "$http_user_agent"';

    server {
        listen 8081;
        server_name myServer1;

        access_log myweb/server1/log/access.log;
        error_page 404 /404.html;

        location /server1/location1 {
            root myweb;
            index index.svr1-loc1.htm;
        }

        location /server1/location2 {
            root myweb;
            index index.svr1-loc2.htm;
        }
    }

    server {
        listen 8082;
        server_name 192.168.0.254;

        auth_basic "please Login:";
        auth_basic_user_file /home/ganiks/learn/nginx/Nginx/myweb/user_passwd;

        access_log myweb/server2/log/access.log;
        error_page 404 /404.html;

        location /server2/location1 {
            root myweb;
            index index.svr2-loc1.htm;
        }

        location /svr2/loc2 {
            alias myweb/server2/location2/;
            index index.svr2-loc2.htm;
        }

        location = /404.html {
            root myweb/;
            index 404.html;
        }
    }
}
ganiks ➜  Nginx git:(master) ✗ ./sbin/nginx -c conf/nginx02.conf
nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /home/ganiks/learn/nginx/Nginx/conf/nginx02.conf:1
ganiks ➜  myweb git:(master) ✗ tree .      
.
├── 404.html
├── server1
│   ├── location1
│   │   └── index.svr1-loc1.htm
│   ├── location2
│   │   └── index.svr1-loc2.htm
│   └── log
│       └── access.log
└── server2
    ├── location1
        │   └── index.svr2-loc1.htm
            ├── location2
                │   └── index.svr2-loc2.htm
                    └── log
                            └── access.log

                            8 directories, 7 files

測試myServer1的訪問

http://myserver1:8081/server1/location1/
this is server1/location1/index.svr1-loc1.htm

http://myserver1:8081/server1/location2/
this is server1/location1/index.svr1-loc2.htm

測試myServer2的訪問

http://192.168.0.254:8082/server2/location1/
this is server2/location1/index.svr2-loc1.htm

http://192.168.0.254:8082/svr2/loc2/
this is server2/location1/index.svr2-loc2.htm

http://192.168.0.254:8082/server2/location2/
404 404 404 404
相關文章
相關標籤/搜索