小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Nginx簡介及配置文件詳解

 紫梔 2017-08-03
一 Nginx簡介
   Nginx是一款開源代碼的高性能HTTP服務(wù)器反向代理服務(wù)器,同時支持IMAP/POP3/SMTP代理服務(wù)
   1.Nginx工作原理
       Nginx由內(nèi)核模塊組成,完成工作是通過查找配置文件將客戶端請求映射到一個location block(location是用于URL匹配的命令),location配置的命令會啟動不同模塊完成工作。
       Nginx模塊分為核心模塊,基礎(chǔ)模塊和第三方模塊。
           核心模塊:HTTP模塊、EVENT模塊(事件)、MAIL模塊。
           基礎(chǔ)模塊:HTTP Access模塊、HTTP FastCGI模塊、HTTP Proxy模塊、HTTP Rewrite模塊。
           第三方模塊:HTTP Upstream Request Hash模塊、Notice模塊、HTTP Access Key模塊。
  
   2.性能優(yōu)勢
       web服務(wù)器,處理靜態(tài)文件、索引文件以及自動索引效率高。
       代理服務(wù)器,快速高效反向代理,提升網(wǎng)站性能。
       負載均衡器,內(nèi)部支持Rails和PHP,也可支持HTTP代理服務(wù)器,對外進行服務(wù)。同時支持簡單容錯和利用算法進行負載均衡。
       性能方面,Nginx專門為性能設(shè)計,實現(xiàn)注重效率。采用Poll模型,可以支持更多的并發(fā)連接,并在大并發(fā)時占用很低內(nèi)存。
       穩(wěn)定性方面,采用分階段資源分配技術(shù),使CPU資源占用率低
       高可用性方面,支持熱備,啟動迅速。
二 Nginx編譯安裝,配置文件詳解
   1.Nginx安裝
  1. #安裝部署nginx所用到的安裝工具和相關(guān)庫
  2. #默認安裝的http_rewrite_module(使用正則對請求重寫)需pcre庫
  3. #默認安裝的httP_gzip_module(Gzip壓縮)需zlib庫
  4. #安裝http_ssl_module(HTTPS/SLL)需openssl庫
  5. yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
  6. #下載nginx源碼包,并解壓
  7. wget http://nginx.org/download/nginx-1.10.3.tar.gz
  8. tar -zxvf nginx-1.10.3.tar.gz
  9. cd nginx-1.10.3
  10. #設(shè)置參數(shù) 參數(shù)具體參考《Nginx編譯參數(shù)》
  11. ./configure \
  12. --prefix=/usr/local/nginx \
  13. --with-http_ssl_module
  14. #編譯并安裝 
  15. make && make install
   2.Nginx配置文件(/usr/local/nginx/conf/nginx.conf)
  
   配置文件主要由四部分組成:main(全區(qū)設(shè)置),server(主機配置),upstream(負載均衡服務(wù)器設(shè)置),和location(URL匹配特定位置設(shè)置)。
   1)全局變量
  1. #Nginx的worker進程運行用戶以及用戶組
  2. #user nobody nobody;
  3. #Nginx開啟的進程數(shù)
  4. worker_processes 1;
  5. #worker_processes auto;
  6. #以下參數(shù)指定了哪個cpu分配給哪個進程,一般來說不用特殊指定。如果一定要設(shè)的話,用0和1指定分配方式.
  7. #這樣設(shè)就是給1-4個進程分配單獨的核來運行,出現(xiàn)第5個進程是就是隨機分配了。eg:
  8. #worker_processes 4 #4核CPU
  9. #worker_cpu_affinity 0001 0010 0100 1000
  10. #定義全局錯誤日志定義類型,[debug|info|notice|warn|crit]
  11. #error_log logs/error.log info;
  12. #指定進程ID存儲文件位置
  13. #pid logs/nginx.pid;
  14. #一個nginx進程打開的最多文件描述符數(shù)目,理論值應(yīng)該是最多打開文件數(shù)(ulimit -n)與nginx進程數(shù)相除,但是nginx分配請求并不是那么均勻,所以最好與ulimit -n的值保持一致。
  15. #vim /etc/security/limits.conf
  16. # * soft nproc 65535
  17. # * hard nproc 65535
  18. # * soft nofile 65535
  19. # * hard nofile 65535
  20. worker_rlimit_nofile 65535;
    2)事件配置
  1. events {
  2. #use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內(nèi)核中的高性能網(wǎng)絡(luò)I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
  3. use epoll;
  4. #每個進程可以處理的最大連接數(shù),理論上每臺nginx服務(wù)器的最大連接數(shù)為worker_processes*worker_connections。理論值:worker_rlimit_nofile/worker_processes
  5. #注意:最大客戶數(shù)也由系統(tǒng)的可用socket連接數(shù)限制(~ 64K),所以設(shè)置不切實際的高沒什么好處
  6. worker_connections 65535;
  7. #worker工作方式:串行(一定程度降低負載,但服務(wù)器吞吐量大時,關(guān)閉使用并行方式)
  8. #multi_accept on;
  9. }
   3)http參數(shù)
  1. #文件擴展名與文件類型映射表
  2. include mime.types;
  3. #默認文件類型
  4. default_type application/octet-stream;
  5. #日志相關(guān)定義
  6. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  7. # '$status $body_bytes_sent "$http_referer" '
  8. # '"$http_user_agent" "$http_x_forwarded_for"';
  9. #定義日志的格式。后面定義要輸出的內(nèi)容。
  10. #1.$remote_addr 與$http_x_forwarded_for 用以記錄客戶端的ip地址;
  11. #2.$remote_user :用來記錄客戶端用戶名稱;
  12. #3.$time_local :用來記錄訪問時間與時區(qū);
  13. #4.$request :用來記錄請求的url與http協(xié)議;
  14. #5.$status :用來記錄請求狀態(tài);
  15. #6.$body_bytes_sent :記錄發(fā)送給客戶端文件主體內(nèi)容大??;
  16. #7.$http_referer :用來記錄從那個頁面鏈接訪問過來的;
  17. #8.$http_user_agent :記錄客戶端瀏覽器的相關(guān)信息
  18. #連接日志的路徑,指定的日志格式放在最后。
  19. #access_log logs/access.log main;
  20. #只記錄更為嚴重的錯誤日志,減少IO壓力
  21. error_log logs/error.log crit;
  22. #關(guān)閉日志
  23. #access_log off;
  24. #默認編碼
  25. #charset utf-8;
  26. #服務(wù)器名字的hash表大小
  27. server_names_hash_bucket_size 128;
  28. #客戶端請求單個文件的最大字節(jié)數(shù)
  29. client_max_body_size 8m;
  30. #指定來自客戶端請求頭的hearerbuffer大小
  31. client_header_buffer_size 32k;
  32. #指定客戶端請求中較大的消息頭的緩存最大數(shù)量和大小。
  33. large_client_header_buffers 4 64k;
  34. #開啟高效傳輸模式。
  35. sendfile on;
  36. #防止網(wǎng)絡(luò)阻塞
  37. tcp_nopush on;
  38. tcp_nodelay on;    
  39. #客戶端連接超時時間,單位是秒
  40. keepalive_timeout 60;
  41.    #客戶端請求頭讀取超時時間
  42.    client_header_timeout 10;
  43.    #設(shè)置客戶端請求主體讀取超時時間
  44.    client_body_timeout 10;
  45.    #響應(yīng)客戶端超時時間
  46.   send_timeout 10;
  47. #FastCGI相關(guān)參數(shù)是為了改善網(wǎng)站的性能:減少資源占用,提高訪問速度。
  48. fastcgi_connect_timeout 300;
  49. fastcgi_send_timeout 300;
  50. fastcgi_read_timeout 300;
  51. fastcgi_buffer_size 64k;
  52. fastcgi_buffers 4 64k;
  53. fastcgi_busy_buffers_size 128k;
  54. fastcgi_temp_file_write_size 128k;
  55. #gzip模塊設(shè)置
  56. #開啟gzip壓縮輸出
  57. gzip on;
  58. #最小壓縮文件大小
  59. gzip_min_length 1k;
  60. #壓縮緩沖區(qū)
  61. gzip_buffers 4 16k;
  62. #壓縮版本(默認1.1,前端如果是squid2.5請使用1.0)
  63. gzip_http_version 1.0;
  64. #壓縮等級 1-9 等級越高,壓縮效果越好,節(jié)約寬帶,但CPU消耗大
  65. gzip_comp_level 2;
  66. #壓縮類型,默認就已經(jīng)包含text/html,所以下面就不用再寫了,寫上去也不會有問題,但是會有一個warn。
  67. gzip_types text/plain application/x-javascript text/css application/xml;
  68.    #前端緩存服務(wù)器緩存經(jīng)過壓縮的頁面
  69.  gzip_vary on;
    4)虛擬主機基本設(shè)置
  1. #虛擬主機定義
  2. server {
  3.        #監(jiān)聽端口
  4. listen 80;
  5.        #訪問域名
  6. server_name localhost;
  7.        #編碼格式,若網(wǎng)頁格式與此不同,將被自動轉(zhuǎn)碼
  8. #charset koi8-r;
  9.        #虛擬主機訪問日志定義
  10. #access_log logs/host.access.log main;
  11.        #對URL進行匹配
  12. location / {
  13.            #訪問路徑,可相對也可絕對路徑
  14. root html;
  15.            #首頁文件。以下按順序匹配
  16. index index.html index.htm;
  17. }
  18. #錯誤信息返回頁面
  19. #error_page 404 /404.html;
  20. # redirect server error pages to the static page /50x.html
  21. #
  22. error_page 500 502 503 504 /50x.html;
  23. location = /50x.html {
  24. root html;
  25. }
  26. #訪問URL以.php結(jié)尾則自動轉(zhuǎn)交給127.0.0.1
  27. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  28. #
  29. #location ~ \.php$ {
  30. # proxy_pass http://127.0.0.1;
  31. #}
  32. #php腳本請求全部轉(zhuǎn)發(fā)給FastCGI處理
  33. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  34. #
  35. #location ~ \.php$ {
  36. # root html;
  37. # fastcgi_pass 127.0.0.1:9000;
  38. # fastcgi_index index.php;
  39. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  40. # include fastcgi_params;
  41. #}
  42. #禁止訪問.ht頁面 (需ngx_http_access_module模塊)
  43. # deny access to .htaccess files, if Apache's document root
  44. # concurs with nginx's one
  45. #
  46. #location ~ /\.ht {
  47. # deny all;
  48. #}
  49. }
  50. #HTTPS虛擬主機定義
  51. # HTTPS server
  52. #
  53. #server {
  54. # listen 443 ssl;
  55. # server_name localhost;
  56. # ssl_certificate cert.pem;
  57. # ssl_certificate_key cert.key;
  58. # ssl_session_cache shared:SSL:1m;
  59. # ssl_session_timeout 5m;
  60. # ssl_ciphers HIGH:!aNULL:!MD5;
  61. # ssl_prefer_server_ciphers on;
  62. # location / {
  63. # root html;
  64. # index index.html index.htm;
  65. # }
  66. #}
    5)Nignx狀態(tài)監(jiān)控
  1. #Nginx運行狀態(tài),StubStatus模塊獲取Nginx自啟動的工作狀態(tài)(編譯時要開啟對應(yīng)功能)
  2. #location /NginxStatus {
  3. #    #啟用StubStatus的工作訪問狀態(tài)    
  4. #    stub_status    on;
  5.        #    #指定StubStaus模塊的訪問日志文件
  6. #    access_log    logs/Nginxstatus.log;
  7.     #    #Nginx認證機制(需Apache的htpasswd命令生成)
  8. #    #auth_basic    "NginxStatus";
  9.     #    #用來認證的密碼文件
  10. #    #auth_basic_user_file    ../htpasswd;
  11. #}
  12. 訪問:http://IP/NginxStatus(測試就不加密碼驗證相關(guān))
    6)反向代理
  1. #以下配置追加在HTTP的全局變量中
  2. #nginx跟后端服務(wù)器連接超時時間(代理連接超時)
  3. proxy_connect_timeout 5;
  4. #后端服務(wù)器數(shù)據(jù)回傳時間(代理發(fā)送超時)
  5. proxy_send_timeout 5;
  6. #連接成功后,后端服務(wù)器響應(yīng)時間(代理接收超時)
  7. proxy_read_timeout 60;
  8. #設(shè)置代理服務(wù)器(nginx)保存用戶頭信息的緩沖區(qū)大小
  9. proxy_buffer_size 16k;
  10. #proxy_buffers緩沖區(qū),網(wǎng)頁平均在32k以下的話,這樣設(shè)置
  11. proxy_buffers 4 32k;
  12. #高負荷下緩沖大?。╬roxy_buffers*2)
  13. proxy_busy_buffers_size 64k;
  14. #設(shè)定緩存文件夾大小,大于這個值,將從upstream服務(wù)器傳
  15. proxy_temp_file_write_size 64k;
  16. #反向代理緩存目錄
  17. proxy_cache_path /data/proxy/cache levels=1:2 keys_zone=cache_one:500m inactive=1d max_size=1g;
  18. #levels=1:2 設(shè)置目錄深度,第一層目錄是1個字符,第2層是2個字符
  19. #keys_zone:設(shè)置web緩存名稱和內(nèi)存緩存空間大小
  20. #inactive:自動清除緩存文件時間。
  21. #max_size:硬盤空間最大可使用值。
  22. #指定臨時緩存文件的存儲路徑(路徑需和上面路徑在同一分區(qū))
  23. proxy_temp_path /data/proxy/temp
  24. #服務(wù)配置
  25. server {
  26. #偵聽的80端口
  27. listen 80;
  28. server_name localhost;
  29. location / {
  30.        #反向代理緩存設(shè)置命令(proxy_cache zone|off,默認關(guān)閉所以要設(shè)置)
  31.        proxy_cache cache_one;
  32.        #對不同的狀態(tài)碼緩存不同時間
  33.        proxy_cache_valid 200 304 12h;
  34.        #設(shè)置以什么樣參數(shù)獲取緩存文件名
  35.        proxy_cache_key $host$uri$is_args$args;
  36. #后7端的Web服務(wù)器可以通過X-Forwarded-For獲取用戶真實IP
  37. proxy_set_header Host $host;
  38. proxy_set_header X-Real-IP $remote_addr;
  39. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  40. #代理設(shè)置
  41. proxy_pass http://IP;
  42.        #文件過期時間控制
  43. expires    1d;
  44. }
  45.    #配置手動清楚緩存(實現(xiàn)此功能需第三方模塊 ngx_cache_purge)
  46.    #http://www./2017/0316/17.html訪問
  47. #http://www./purge/2017/0316/17.html清楚URL緩存
  48. location ~ /purge(/.*) {
  49. allow    127.0.0.1;
  50. deny    all;
  51. proxy_cache_purge    cache_one    $host$1$is_args$args;
  52. }
  53. #設(shè)置擴展名以.jsp、.php、.jspx結(jié)尾的動態(tài)應(yīng)用程序不做緩存
  54.    location ~.*\.(jsp|php|jspx)?$ {
  55. proxy_set_header Host $host;
  56. proxy_set_header X-Real-IP $remote_addr;
  57. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  58. proxy_pass http://http://IP;
  59. }
    7)負載均衡
  1. #負載均衡服務(wù)器池
  2. upstream my_server_pool {
  3.    #調(diào)度算法
  4.    #1.輪循(默認)(weight輪循權(quán)值)
  5.    #2.ip_hash:根據(jù)每個請求訪問IP的hash結(jié)果分配。(會話保持)
  6.    #3.fair:根據(jù)后端服務(wù)器響應(yīng)時間最短請求。(upstream_fair模塊)
  7.    #4.url_hash:根據(jù)訪問的url的hash結(jié)果分配。(需hash軟件包)
  8.    #參數(shù):
  9.    #down:表示不參與負載均衡
  10.    #backup:備份服務(wù)器
  11.    #max_fails:允許最大請求錯誤次數(shù)
  12.    #fail_timeout:請求失敗后暫停服務(wù)時間。
  13. server 192.168.1.109:80 weight=1 max_fails=2 fail_timeout=30;
  14. server 192.168.1.108:80 weight=2 max_fails=2 fail_timeout=30;
  15. }
  16. #負載均衡調(diào)用
  17. server {
  18. ...
  19.    location / {
  20. proxy_pass http://my_server_pool;
  21. }
  22. }
    8)URL重寫
  1. #根據(jù)不同的瀏覽器URL重寫
  2. if($http_user_agent ~ Firefox){
  3. rewrite ^(.*)$ /firefox/$1 break;
  4. }
  5. if($http_user_agent ~ MSIE){
  6. rewrite ^(.*)$ /msie/$1 break;
  7. }
  8. #實現(xiàn)域名跳轉(zhuǎn)
  9. location / {
  10. rewrite ^/(.*)$ https://web8.example.com$1 permanent;
  11. }
    9)IP限制
  1. #限制IP訪問
  2. location / {
  3. deny 192.168.0.2
  4. allow 192.168.0.0/24;
  5. allow 192.168.1.1;
  6. deny all;
  7. }
    10)Nginx相關(guān)命令
  1. #啟動nginx
  2. nginx
  3. #關(guān)閉nginx
  4. nginx -s stop
  5. #平滑重啟
  6. kill -HUP `cat /usr/local/nginx/logs/nginx.pid`
    11)Nginx啟動腳本
  1. #!/bin/bash
  2. #chkconfig: 2345 80 90 #description:auto_run
  3. PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
  4. export PATH
  5. # Check if user is root
  6. if [ $(id -u) != "0" ]; then
  7. echo "Error: You must be root to run this script!\n"
  8. exit 1
  9. fi
  10. NGINXDAEMON=/usr/local/nginx/sbin/nginx
  11. PIDFILE=/usr/local/nginx/logs/nginx.pid
  12. function_start()
  13. {
  14. echo -en "\033[32;49;1mStarting nginx......\n"
  15. echo -en "\033[39;49;0m"
  16. if [ -f $PIDFILE ]; then
  17. printf "Nginx is runing!\n"
  18. exit 1
  19. else
  20. $NGINXDAEMON
  21. printf "Nginx is the successful start!\n"
  22. fi
  23. }
  24. function_stop()
  25. {
  26. echo -en "\033[32;49;1mStoping nginx......\n"
  27. echo -en "\033[39;49;0m"
  28. if [ -f $PIDFILE ]; then
  29. kill `cat $PIDFILE`
  30. printf "Nginx program is stoped\n"
  31. else
  32. printf "Nginx program is not runing!\n"
  33. fi
  34. }
  35. function_reload()
  36. {
  37. echo -en "\033[32;49;1mReload nginx......\n"
  38. echo -en "\033[39;49;0m"
  39. function_stop function_start
  40. }
  41. function_restart()
  42. {
  43. echo -en "\033[32;49;1mRestart nginx......\n"
  44. echo -en "\033[39;49;0m"
  45. printf "Reload Nginx configure...\n"
  46. $NGINXDAEMON -t
  47. kill -HUP `cat $PIDFILE`
  48. printf "Nginx program is reloding!\n"
  49. }
  50. function_kill()
  51. {
  52. killall nginx
  53. }
  54. function_status()
  55. {
  56. if ! ps -ef|grep -v grep|grep 'nginx:' > /dev/null 2>&1
  57. then
  58. printf "Nginx is down!!!\n"
  59. else
  60. printf "Nginx is running now!\n"
  61. fi
  62. }
  63. if [ "$1" = "start" ]; then
  64. function_start
  65. elif [ "$1" = "stop" ]; then
  66. function_stop
  67. elif [ "$1" = "reload" ]; then
  68. function_reload
  69. elif [ "$1" = "restart" ]; then
  70. function_restart
  71. elif [ "$1" = "kill" ]; then
  72. function_kill
  73. elif [ "$1" = "status" ]; then
  74. function_status
  75. else
  76. echo -en "\033[32;49;1m Usage: nginx {start|stop|reload|restart|kill|status}\n"
  77. echo -en "\033[39;49;0m"
  78. fi



    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多