Nginx 配置文件

1. 虛擬主機配置

1.1. 啓用虛擬主機配置:
vim /opt/nginx/conf/nginx.conf
刪除server 主機配置文件,增長include vhost/*.conf;
1.2. 建立虛擬主機配置存放目錄:mkdir /opt/nginx/conf/vhost
1.3. 建立虛擬主機配置:
cd !$; vim default.confphp

server
{
   listen 80;      # 監聽的端口
   server_name localhost;    # 監聽的域名
   index index.html index.htm index.php; 
   root /opt/www/html;  # 網頁路徑
}

2.Nginx 用戶認證

2.1. 安裝http
yum install -y httpd(用來建立用戶、密碼)
2.2. 建立用戶、密碼
htpasswd -c /opt/nginx/conf/htpasswd admin (運行‘htpasswd’建立用戶名、密碼時,不能在用戶名、密碼存放的目錄下運行該命令;這裏不能在‘conf’目錄下運行‘htpasswd’)
2.3. 虛擬主機中的配置css

location /        # 做用整個網站:「/」,做用某個目錄: 「/admin/」,做用某個url:"~ admin.php" ~匹配
    {
     auth_basic            "Auth";
     auth_basic_user_file   /opt/nginx/conf/htpasswd;  # 定義用戶認證的用戶名、密碼路徑;
    }

2.4. 檢查Nginx配置&從新加載Nginx配置
/opt/nginx/sbin/nginx -t 檢查Nginx配置;
/opt/nginx/sbin/nginx -s reload 從新加載Nginx;
若以前運行過上述命令,則可按「Ctrl+R」建,直接輸入 -t && -s reload
2.5. 驗證
curl -x127.0.0.1:80 test.com -I 正常會出現401報錯信息,須要指定用戶名、密碼;
curl -uadmin:admin -x127.0.0.1:80 test.com -I 正常會出現200 的代碼,這個說明用戶認證已經生效了;html

3.Nginx 域名重定向

3.1. 更改虛擬主機配置文件,如:test.com.confnginx

server
{
    listen 80;
    server_name test.com test1.com test2.com;        # 指定域名,這裏能夠指定多個域名
    index index.html index.htm index.php;
    root /data/wwwroot/test.com;
    if ($host != 'test.com' )      #  if 在這裏是用來重定向的,這裏是將全部的域名都重定向到 test.com
       {
         rewrite  ^/(.*)$  http://test.com/$1  permanent;     # 「rewrite」 跳轉,「 ^/(.*)$ 」 以「/」開頭任意結尾;permanent  301跳轉、redirect 302 跳轉;
       }
}

4. Nginx訪問日誌

4.1. 主配置文件「nginx.conf」中搜索「log_format」,這個是定義log樣式的;默認日誌樣式以下:web

log_format combined_realip '$remote_addr $http_x_forwarded_for [$time_local]'
    ' $host "$request_uri" $status'
    ' "$http_referer" "$http_user_agent"';

4.2. 日誌參數說明:算法

  • combined_realip 日誌格式名稱,能夠自定義;
  • $remote_addr 客戶端IP(公網IP);
  • $http_x_forwarded_for 代理服務器的IP;
  • $time_local 服務器本地時間;
  • $host 訪問主機名(域名);
  • $request_uri 訪問的url地址;
  • $status 狀態碼 (如:40四、30一、30二、200等);
  • $http_referer
  • $http_user_agent

4.3. 虛擬主機日誌配置
除了在主配置文件「nginx.conf」裏定義日誌格式外,還須要在虛擬主機配置文件中增長:access_log /tmp/1.log combined_realip;
這裏的「combined_realip」就是在nginx.conf中定義的日誌格式名字;shell

4.4. 檢查配置,從新加載Nginx配置
-t && -s reload
curl -x127.0.0.1:80 test.com -I
cat /tmp/1.logvim

5. Nginx日誌切割

5.1. 自定義shell 腳本,用於日誌切割;
vim /usr/local/sbin/nginx_log_rotate.sh//寫入以下內容瀏覽器

#! /bin/bash
## 假設nginx的日誌存放路徑爲/data/logs/
d=`date -d "-1 day" +%Y%m%d` 
logdir="/data/logs"
nginx_pid="/usr/local/nginx/logs/nginx.pid"
cd $logdir
for log in `ls *.log`
do
    mv $log $log-$d
done
/bin/kill -HUP `cat $nginx_pid`

5.2. 任務計劃
crontab -e
添加定時任務
0 0 * * * /bin/bash /usr/local/sbin/nginx_log_rotate.sh緩存

6. 靜態文件不記錄日誌和過時時間

6.1. 具體配置以下:

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
          expires      7d;    # 緩存過時時間
          access_log off;   # 關閉日誌記錄
    }
location ~ .*\.(js|css)$
    {
          expires      12h;
          access_log off;
    }

7. Nginx防盜鏈

7.1. 配置以下,能夠和上面的配置結合起來

location ~* ^.+\.(gif|jpg|png|swf|flv|rar|zip|doc|pdf|gz|bz2|jpeg|bmp|xls)$
{
    expires 7d;  # 緩存過時時間
    valid_referers none blocked server_names  *.test.com ;
    if ($invalid_referer) {
        return 403;
    }
    access_log off;  # 關閉日誌記錄
}

8. Nginx 訪問控制

8.1. 需求:訪問/admin/目錄的請求,只容許某幾個IP訪問,配置以下:

location /admin/
{
    allow 192.168.133.1;
    allow 127.0.0.1;
    deny all;
}
  • 建立目錄mkdir /data/wwwroot/test.com/admin/
  • 建立網頁echo 「test,test」>/data/wwwroot/test.com/admin/1.html
  • 檢查&加載配置-t && -s reload
  • 測試訪問:
    curl -x127.0.0.1:80 test.com/admin/1.html -I
    curl -x192.168.133.130:80 test.com/admin/1.html -I

8.2. 能夠匹配正則

location ~ .*(abc|image)/.*\.php$   
{
       deny all;
}

8.3. 根據user_agent限制

if ($http_user_agent ~ 'Spider/3.0|YoudaoBot|Tomato')  
{
      return 403;  # 「deny all」和「return 403」效果同樣
}

9. Nginx 解析PHP配置

9.1. 配置以下:

location ~ \.php$
    {
        include fastcgi_params;
        fastcgi_pass unix:/tmp/php-fcgi.sock;
        # fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME /opt/www/html$fastcgi_script_name;
    }

fastcgi_pass 用來指定php-fpm監聽的「地址:端口」或者socket

10. Nginx 代理

10.1. Nginx 代理示意圖:

10.2. Nginx 代理配置:
cd /usr/local/nginx/conf/vhost
vim proxy.conf //加入以下配置

server
{
    listen 80;      # 監聽端口
    server_name ask.apelearn.com;    # 訪問域名

    location /
    {
        proxy_pass      http://121.201.9.155/;     # 代理的網站IP地址
        proxy_set_header Host   $host;             # 代理的網站域名,Host == server_name
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

11. Nginx 負載均衡

負載均衡配置:
vim /usr/local/nginx/conf/vhost/load.conf // 寫入以下內容

upstream qq_com    #  「upstream」來指定多個「web server」
{
    ip_hash;   # 做用:確保同個客戶端訪問在同一個IP
    server 61.135.157.156:80;
    server 125.39.240.113:80;
}
server
{
    listen 80;
    server_name www.qq.com;
    location /
    {
        proxy_pass      http://qq_com;
        proxy_set_header Host   $host;
        proxy_set_header X-Real-IP      $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Nginx 不支持代理 https ;但能夠在Nginx代理上設置https 監聽

12. Nginx配置SSL(https 訪問)

12.1. SSL工做流程

  1. 瀏覽器發送一個https的請求給服務器;
  2. 服務器要有一套數字證書,能夠本身製做(後面的操做會具體介紹),也能夠向組織申請,區別就是本身頒發的證書須要客戶端驗證經過,才能夠繼續訪問,而使用受信任的公司申請的證書,則不會彈出>提示頁面,這套證書其實就是一對公鑰和私鑰;
  3. 服務器會把公鑰傳輸給客戶端;
  4. 客戶端(瀏覽器)收到公鑰後,會驗證其是否合法有效,無效會有警告提醒,有效則會生成一串隨機數,並用收到的公鑰加密;
  5. 客戶端把加密後的隨機字符串傳輸給服務器;
  6. 服務器收到加密隨機字符串後,先用私鑰解密(公鑰加密,私鑰解密),獲取到這一串隨機數後,再用這串隨機字符串加密傳輸的數據(該加密爲對稱加密,所謂對稱加密,就是將數據和私鑰也就是這個隨機字符串>經過某種算法混合在一塊兒,這樣除非知道私鑰,不然沒法獲取數據內容);
  7. 服務器把加密後的數據傳輸給客戶端;
  8. 客戶端收到數據後,再用本身的私鑰也就是那個隨機字符串解密;

12.2 SSL 工做示意圖

12.3. 生成SSL密鑰對

  • 進入「conf」目錄
    cd /usr/local/nginx/conf
  • 建立私鑰
    openssl genrsa -des3 -out tmp.key 2048 //key文件爲私鑰
  • 轉換key,取消密碼
    openssl rsa -in tmp.key -out admin.key //轉換key,取消密碼
    rm -f tmp.key
  • 生成證書請求文件
    openssl req -new -key admin.key -out admin.csr//生成證書請求文件,須要拿這個文件和私鑰一塊兒生產公鑰文件
  • 生成公鑰文件
    openssl x509 -req -days 365 -in admin.csr -signkey admin.key -out admin.crt // 這裏的admin.crt爲公鑰

12.4. Nginx配置 SSL

  • 編輯配置:vim /usr/local/nginx/conf/vhost/ssl.conf//加入以下內容
server
{
    listen 443;
    server_name admin.com;
    index index.html index.php;
    root /data/wwwroot/admin.com;
    ssl on;
    ssl_certificate admin.crt;
    ssl_certificate_key admin.key;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
}
  • 檢查&加載配置
    -t && -s reload //若報錯unknown directive 「ssl」 ,須要從新編譯nginx,加上「--with-http_ssl_module」
  • 建立目錄、網頁:
    mkdir /data/wwwroot/aming.com
    echo 「ssl test page.」>/data/wwwroot/admin.com/index.html
  • Windows測試網頁:
    編輯hosts,增長127.0.0.1 admin.com
    瀏覽器訪問: https://admin.com/
相關文章
相關標籤/搜索