Nginx配置詳解

源引:http://www.cnblogs.com/knowledgesea/p/5175711.html

Nginx常用功能

一、代理:尤其是反向代理。

正向代理與反向代理

二、負(fù)載均衡

負(fù)載均衡策略有2種:

  • 內(nèi)置策略
  • 擴(kuò)展策略

1、內(nèi)置策略

1)輪詢 、加權(quán)輪詢。
輪詢和加權(quán)輪詢
2) IP hash算法

對(duì)客戶端請(qǐng)求的ip進(jìn)行hash操作,然后根據(jù)hash結(jié)果將同一個(gè)客戶端ip的請(qǐng)求分發(fā)給同一臺(tái)服務(wù)器進(jìn)行處理,可以解決session不共享的問題。

IP hash算法

2、擴(kuò)展策略

你可以參照所有的負(fù)載均衡算法,給他一一找出來并做實(shí)現(xiàn)。

3、web緩存

Nginx可以對(duì)不同的文件做不同的緩存處理,配置靈活,并且支持FastCGI_Cache,主要用于對(duì)FastCGI的動(dòng)態(tài)程序進(jìn)行緩存。配合著第三方的ngx_cache_purge,對(duì)制定的URL緩存內(nèi)容可以的進(jìn)行增刪管理。

4、Nginx相關(guān)地址

源碼:https://trac.nginx.org/nginx/browser
官網(wǎng):http://www.nginx.org/

三、Nginx配置文件結(jié)構(gòu)

打開conf文件夾的nginx.conf文件,Nginx服務(wù)器的基礎(chǔ)配置,默認(rèn)的配置也存放在此。

1、默認(rèn)配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events { 
    worker_connections 1024;
}
http {
     include mime.types; 
    default_type application/octet-stream;
     #    log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 
     #                               ' $status $body_bytes_sent "$http_referer" ' 
     #                                ' "$http_user_agent"  "$http_x_forwarded_for" ';
     #access_log logs/access.log main; 
     sendfile on; 
     #tcp_nopush on; 

     #keepalive_timeout 0; 
     keepalive_timeout 65; 
     #gzip on; 
      server { 
          listen 80; 
          server_name localhost; 
         #charset koi8-r; 
         #access_log logs/host.access.log main; 
         location / { 
               root     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; 
         #} 
     } 
     # another virtual host using mix of IP-, name-, and port-based configuration 
     # 
     #  server   { 
     #       listen 8000; 
     #       listen somename:8080; 
     #       server_name somename alias another.alias; 
     #       location   / { 
     #           root html; 
     #           index index.html index.htm; 
     #       } 
     #  } 
     #   HTTPS server 
     # 
     #  server { 
     #       listen 443 ssl; 
     #       server_name localhost; 
     #       ssl_certificate cert.pem; 
     #       ssl_certificate_key cert.key; 
     #       ssl_session_cache shared:SSL:1m; 
     #       ssl_session_timeout 5m; 
     #       ssl_ciphers HIGH:!aNULL:!MD5; 
     #       ssl_prefer_server_ciphers on; 
     #       location   /   { 
     #             root html; 
     #             index index.html index.htm; 
     #       } 
     # }
}
2、nginx文件結(jié)構(gòu)
#全局塊
... 
#events塊 
events {  
      ...
}
#http塊
http {
      #http全局塊 
      ... 
      #server塊 
      server   {    
            #server全局塊
            ......
            #location塊 
            location [PATTERN] {      
                  ...... 
            } 
            location [PATTERN] { 
                 ... 
             } 
       } 
       server { 
                  ... 
        }
    #http全局塊
    ...     
}
1)、全局塊:

配置影響nginx全局的指令。一般有:

  • 運(yùn)行nginx服務(wù)器的用戶組;
  • nginx進(jìn)程pid存放路徑;
  • 日志存放路徑;
  • 配置文件引入;
  • 允許生成worker process數(shù)等
2)、events塊:

配置影響nginx服務(wù)器或與用戶的網(wǎng)絡(luò)連接。

  • 每個(gè)進(jìn)程的最大連接數(shù)
  • 選取哪種事件驅(qū)動(dòng)模型處理連接請(qǐng)求
  • 是否允許同時(shí)接受多個(gè)網(wǎng)路連接
  • 開啟多個(gè)網(wǎng)絡(luò)連接序列化等。
3、http塊:

可以嵌套多個(gè)server,配置代理,緩存,日志定義等絕大多數(shù)功能和第三方模塊的配置。
如文件引入,mime-type定義,日志自定義,是否使用sendfile傳輸文件,連接超時(shí)時(shí)間,單連接請(qǐng)求數(shù)等。

4、server塊:

配置虛擬主機(jī)的相關(guān)參數(shù),一個(gè)http中可以有多個(gè)server。

5、location塊:

配置請(qǐng)求的路由,以及各種頁面的處理情況。

########### 每個(gè)指令必須有分號(hào)結(jié)束。##################
user administrator administrators;       #配置用戶或者組,默認(rèn)為nobody nobody。
#worker_processes 2;                     #允許生成的進(jìn)程數(shù),默認(rèn)為1
#pid /nginx/pid/nginx.pid;               #指定nginx進(jìn)程運(yùn)行文件存放地址
error_log log/error.log debug;           #指定日志路徑,級(jí)別。這個(gè)設(shè)置可以放入全局塊,http塊,server塊,級(jí)別以此為:
#debug|info|notice|warn|error|crit|alert|emerg
events { 
      accept_mutex on;     #設(shè)置網(wǎng)路連接序列化,防止驚群現(xiàn)象發(fā)生,默認(rèn)為on       
      multi_accept on;     #設(shè)置一個(gè)進(jìn)程是否同時(shí)接受多個(gè)網(wǎng)絡(luò)連接,默認(rèn)為off 
      #use epoll;           #事件驅(qū)動(dòng)模型,select|poll|kqueue|epoll|resig|/dev/poll|eventport   
      worker_connections 1024;   #最大連接數(shù),默認(rèn)為512
}
http   { 
      include mime.types;                       #文件擴(kuò)展名與文件類型映射表 
      default_type application/octet-stream;   #默認(rèn)文件類型,默認(rèn)為text/plain 
      # 配置跨域問題
      add_header Access-Control-Allow-Origin *;  ##全匹配方式。
      add_header Access-Control-Allow-Headers X-Requested-With;
      add_header Access-Control-Allow-Methods GET,POST,OPTIONS;

      #access_log off;                         #取消服務(wù)日志 
      log_format   myFormat   '$remote_addr–$remote_user [$time_local] $request $status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for'; 
      #自定義log格式 
      access_log log/access.log myFormat;       #combined  為日志格式的默認(rèn)值   
      sendfile on;                              #允許sendfile方式傳輸文件,默認(rèn)為off,可以在http塊,server塊,location塊。 
      sendfile_max_chunk 100k;                  #每個(gè)進(jìn)程每次調(diào)用傳輸數(shù)量不能大于設(shè)定的值,默認(rèn)為0,即不設(shè)上限。 
      server_names_hash_bucket_size 64;         #hash表可保存服務(wù)器名字的數(shù)量(32的倍數(shù))
      keepalive_timeout 65;                     #連接超時(shí)時(shí)間,默認(rèn)為75s,可以在http,server,location塊。 
      upstream mysvr { 
            server 127.0.0.1:7878; 
            server 192.168.10.121:3333 
            backup;     #熱備 
      } 
      error_page 404 https://www.baidu.com;     #錯(cuò)誤頁 
      server { 
            keepalive_requests 120;              #單連接請(qǐng)求上限次數(shù)。 
            listen 4545;                         #監(jiān)聽端口 
            server_name 127.0.0.1;               #監(jiān)聽地址,不需要加http:// 
            location   ~*^.+$ {                  #請(qǐng)求的url過濾,正則匹配,~為區(qū)分大小寫,~*為不區(qū)分大小寫。 
                  #root path;                    #根目錄 
                  #index vv.txt;                 #設(shè)置默認(rèn)頁 
                  proxy_pass http://mysvr;       #請(qǐng)求轉(zhuǎn)向mysvr 定義的服務(wù)器列表 
                  deny 127.0.0.1;                #拒絕的ip 
                  allow 172.18.5.54;              #允許的ip 
            } 
       }
} 

上面是nginx的基本配置,需要注意的有以下幾點(diǎn):

1、
  1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址; 
  2.$remote_user :用來記錄客戶端用戶名稱; 
  3.$time_local : 用來記錄訪問時(shí)間與時(shí)區(qū);
  4.$request : 用來記錄請(qǐng)求的url與http協(xié)議;
  5.$status : 用來記錄請(qǐng)求狀態(tài);成功是200, 
  6.$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大小;
  7.$http_referer :用來記錄從那個(gè)頁面鏈接訪問過來的; 
  8.$http_user_agent :記錄客戶端瀏覽器的相關(guān)信息;
2、驚群現(xiàn)象:
  一個(gè)網(wǎng)路連接到來,多個(gè)睡眠的進(jìn)程被同時(shí)叫醒,但只有一個(gè)進(jìn)程能獲得鏈接,這樣會(huì)影響系統(tǒng)性能。
3、每個(gè)指令必須有分號(hào)結(jié)束
4、啟動(dòng)與停止
#啟動(dòng)
$ sudo /etc/init.d/nginx
或
$ nginx    (如果你配置了環(huán)境變量)

# 重啟
$ nginx -s reload

#停止
$ sudo service nginx stop 
或
$ ps -e | grep nginx    #查看進(jìn)程ID,然后
$ kill  進(jìn)程ID

四、常用指令

  • autoindex on; // 指定該目錄下的所有文件可通過瀏覽器直接訪問
示例:
location  /images/ {
    root /var/www/images/;
    autoindex on; 
}

五、安全策略配置

server {
  ###
  #   瀏覽器默認(rèn)會(huì)對(duì)靜態(tài)資源進(jìn)行Content-Type 的猜測
  #  (如果沒設(shè)置,或者故意設(shè)置錯(cuò),比如一個(gè)js文件,缺被黑客設(shè)置成為image/png),
  #  設(shè)置 X-Content-Type-Options "nosniff" ,禁止瀏覽器進(jìn)行猜測。
  ##
  add_header X-Content-Type-Options "nosniff";
  ##
  #  如果發(fā)現(xiàn)有跨站攻擊,則立刻停止頁面服務(wù)
  ##
  add_header X-XSS-Protection "1; mode=block";
  ##
  #  我們的網(wǎng)站發(fā)送請(qǐng)求的時(shí)候,往往在 請(qǐng)求頭中帶上 Referer,
  #  我么可以通過這個(gè)來控制。 可選值自行查詢
  ##
  add_header Referrer-Policy  "no-referrer-when-downgrade";
  add_header X-Permitted-Cross-Domain-Policies 'none';
  add_header X-Download-Options 'noopen';
  ## 
  # 用于控制網(wǎng)站可以在哪些被允許的域名網(wǎng)站中以 <iframe>的形式嵌入
  # DENY :不允許被任何頁面嵌入;
  # SAMEORIGIN :不允許被本域以外的頁面嵌入;
  # ALLOW-FROM uri :不允許被指定的域名以外的頁面嵌入(Chrome現(xiàn)階段不支持);
  ##
  add_header X-Frame-Options  "SAMEORIGIN";
  ##
  # 控制當(dāng)前網(wǎng)站只可以加載 哪里的資源
  # default-src *   表示除了單獨(dú)指定的類型資源以外,其他資源都按照這個(gè)設(shè)置來
  # script-src  https://*.facebook.com http://*.facebook.com  表示 javascript 腳本的資源只能從 https://*.facebook.com http://*.facebook.com 這兩個(gè)地方加載
  # style-src  https://*.facebook.com http://*.facebook.com  表示 css樣式表 的資源只能從 https://*.facebook.com http://*.facebook.com 這兩個(gè)地方加載
  # 還有很多,請(qǐng)自定查詢
  ##
  add_header Content-Security-Policy default-src *;script-src https://*.facebook.com http://*.facebook.com;style-src https://*.facebook.com;
  # strict-transport-security "max-age=31536000;" # 在接下來的365天內(nèi)強(qiáng)制使用https,需檢查自己服務(wù)器是否支持https
  add_header strict-transport-security "max-age=31536000;";
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一章 Nginx簡介 Nginx是什么 沒有聽過Nginx?那么一定聽過它的“同行”Apache吧!Ngi...
    JokerW閱讀 33,029評(píng)論 24 1,002
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,639評(píng)論 19 139
  • 上一篇《WEB請(qǐng)求處理一:瀏覽器請(qǐng)求發(fā)起處理》,我們講述了瀏覽器端請(qǐng)求發(fā)起過程,通過DNS域名解析服務(wù)器IP,并建...
    七寸知架構(gòu)閱讀 81,779評(píng)論 21 356
  • nginx配置文件結(jié)構(gòu) 從文件結(jié)構(gòu)可知配置文件主要由一下六部分組成: main(全局設(shè)置) events(ngin...
    忘凈空閱讀 977評(píng)論 0 0
  • Page 1:nginx 服務(wù)器安裝及配置文件詳解 CentOS 6.2 x86_64 安裝 nginx 1.1 ...
    xiaojianxu閱讀 8,689評(píng)論 1 41

友情鏈接更多精彩內(nèi)容