nginx 配置文件學習

1.全局塊:

主要設置一些影響nginx 服務器總體運行的配置指令。如:用戶(組),生成worker process 數,日誌存放路徑,配置文件引入,pid文件路徑javascript

2.events 塊

影響nginx 服務器與用戶的網絡鏈接。如:每一個worker processs 的最大鏈接數,事件驅動模型css

3.http塊

nginx 配置中的重要部分,代理,緩存等功能均可以在此配置。如:文件引入,mime-type 定義,鏈接超時,單鏈接請求數上限html

4.server 塊

與「虛擬主機」的概念有密切關係。每一個server 塊至關於一臺虛擬主機,最多見的兩個配置項是監聽配置和虛擬主機的名稱或IP配置前端

5.location 塊

在server 塊中,能夠有多個。地址定向,數據緩存,和應答控制等功能都是在這部分實現。java

 

  • listen address[:port];          #配置網絡監聽
    listen *:80 | *:8080  |  8080;   #監聽全部的80 和 8080 端口
    listen 192.168.1.1:8080;        #監聽192.168.1.1地址的8080端口
    listen 192.168.1.11;               #監聽192.168.1.11 的全部端口

 

  • server_name name ....;       #虛擬主機配置
    server_name www.xx.com yy.com;        #虛擬主機名爲www.xx.com 和 yy.com
    server_name *xx.com www.yy.*;            #能夠使用通配符,但只能放在首段或尾段
    server_name ~^www\d+\.xx\.com$     #能夠使用正則表達式,但必須使用 ~ 開頭
    匹配規則按:準確匹配,通配符(開始)匹配 ,通配符(結尾)匹配,正則匹配

 

  • location [ = | ~ | ~* | ^~ ] uri {...}           #位置配置
    在一不使用 =、~、~*、^~ 時的匹配規則:先搜索【標準的uri】若是有多個能夠匹配,那麼就記錄匹配度最高的一個。而後再搜索【正則uri】,當第一個【正則uri】匹配成功時就結束搜索並使用它。若是【正則uri】都沒有匹配到,就使用【標準uri】就是剛搜索的那個。
    【=】  :     只能用在【標準uri】前,若是匹配成功了,就中止搜索並使用它。
    【~ | ~*】:只能用在【正則uri】前,前面區分大小寫,後面不區分大小寫
    【^~】:       只能用在【標準uri】前,搜索全部【標準uri】找到匹配度最高的一個結束搜索並使用它

 

  • root path;    配置請求的根目錄
    path 爲服務器資源的根目錄路徑,如:
    location /data/ {
       root /usr/local/web;  
    }
    當請求爲 /data/index.htm 時, 將映射至 /usr/local/web/data/index.hm

 

  • alias path;   更改location 的URI
    location ~ ^/data/(.+\.(htm|htm))${
        alias /usr/local/app/$1;
    }
    當請求爲 /data/index.htm 時, 將映射至 /usr/local/app/index.htm

 

  • index file ...;    設置網站的默認首頁

 

  • error_page code ... uri  #設置網站的錯誤頁面
    error_page 404  /404.htm
    error_page 403  http://www.xx.com/403.htm
    error_page 410 =301 /fes.htm  # 當服務器產生410時,返回 fes.htm ,並客戶端http status 爲 301

 

  • allow address | CIDR | all;    # 基於ip配置nginx 的訪問權限 - 容許
  • deny address | CIDR | all;     # 基於ip配置nginx 的訪問權限 - 拒絕
    location /  {
       deny 192.168.1.10;
       allow  192.168.2.45;
       allow 192.168.3.78;
       deny all;
    }
    遇到 deny 或 allow 指定是按順序對當前客戶端的鏈接進行訪問權限檢查
  • auth_basic string | off;     基於密碼配置nginx 的訪問權限
  • auth_basic_user_file file;  使用文件來配置用戶名和密碼

 

#=========================全局塊============================

#user user [grop];        # 指定能夠運行nginx 服務器的用戶及組。只被設置的用戶及組纔有權限管理
#user nobody nobody;      # 所用用戶可用,默認配置
user nginx nginx; 

#worker_processes number | auto;   #控制併發處理,值越大,支持的併發數越多
#worker_processes 1;               #默認值 ,最多產生1個worker_processes
worker_processes auto;

#pid file;                  #pid 存放路徑.相對或絕對路徑
#pid logs/nginx.pid;        #默認在安裝目錄logs/nginx.pid
pid /var/run/nginx.pid;

#error_log file | stder [debug | info | notice | warn | error | crit | alert | emerg];  #錯誤日誌存放路徑
#error_log logs/error.log error;  # 可在 全局塊,http 塊 ,serveer 塊,location 塊中配置
error_log /data/wwwlogs/error_nginx.log crit;

#include file;      #配置文件引入,能夠是相對/絕對路徑。   指令能夠放在配置文件的任意地方(任意塊中)

worker_rlimit_nofile 51200;


#===========================events 塊===============================

events {
    #accept_mutex on|off;      #網絡鏈接序列化。解決「驚羣」問題
    accept_mutex on;           #默認爲開啓狀態

    #multi_accept on|off;      #是否容許worker process同時接收多個網絡鏈接,
    multi_accept off;          #默認爲關閉

    #use method;               #事件驅動模型選擇,select 、 poll 、 kqueue 、 epoll 、 rtsig 、 /dev/poll 、 eventport 
    use epoll;

    #worker_connections number; #worker process 的最大鏈接數。 
    worker_connections 512;     #默認值爲512。注意設置不能大於操做系統支持打開的最大文件句柄數量。
}



#==========================http塊============================

http {

    #定義 mime-type (網絡資源的媒體類型)。  指定能識別前端請求的資源類型 
    include mime.types;   #引入外部文件 ,在外部文件中有定義types 塊
    default_type application/octet-stream;   #默認爲 text/plain 。 http塊、server塊、location塊中可配置

    #access_log path [format[buffer=size]];  #定義服務日誌,記錄前端的請求日誌
    #access_log logs/access.log combined;    #combined 是默認定義的日誌格式字符串名稱
    access_log off;                          #關閉日誌


    #log_format name stirng      #定義了日誌格式,以便access_log 使用
    #log_format  combined  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #sendfile on|off;       #配置容許 sendfile 方式傳輸文件
    sendfile off;           #默認關閉,可在http塊、server塊、location塊 中定義

    #sendfile_max_chunk size;    #配置worker process 每次調用sendfile()傳輸的數據最最大值
    #sendfile_max_chunk 0;        #默認爲0 不限制。  可在http塊、server塊、location塊 中定義
    sendfile_max_chunk 128k;

    #keepalive_timeout timeout [header_timeout];    #配置鏈接超時時間, 可在http塊、server塊、location塊 中定義
    #keepalive_timeout 75s;        #默認爲75秒,與用戶創建會話鏈接後,nginx 服務器能夠保持這些鏈接打開的時間。
    keepalive_timeout 120 100s;    #在服務器端保持鏈接的時間設置爲120s,發給用戶端的應答報文頭部中keep-alive 域的設置爲100s

    #keepalive_requests number;    #單鏈接請求數上限
    keepalive_requests 100;        #默認爲 100

    

    server_names_hash_bucket_size 128;
    client_header_buffer_size 32k;
    large_client_header_buffers 4 32k;
    client_max_body_size 1024m;

    tcp_nopush on;

    server_tokens off;
    tcp_nodelay on;
    fastcgi_connect_timeout 300;
    fastcgi_send_timeout 300;
    fastcgi_read_timeout 300;
    fastcgi_buffer_size 64k;
    fastcgi_buffers 4 64k;
    fastcgi_busy_buffers_size 128k;
    fastcgi_temp_file_write_size 128k;

    #Gzip Compression
    gzip on;
    gzip_buffers 16 8k;
    gzip_comp_level 6;
    gzip_http_version 1.1;
    gzip_min_length 256;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
        text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
        text/javascript application/javascript application/x-javascript
        text/x-json application/json application/x-web-app-manifest+json
        text/css text/plain text/x-component
        font/opentype application/x-font-ttf application/vnd.ms-fontobject
        image/x-icon;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
    open_file_cache max=1000 inactive=20s;
    open_file_cache_valid 30s;
    open_file_cache_min_uses 2;
    open_file_cache_errors on;

######################## default ############################
#    server {
#    listen 8067;
#    server_name _;
#    access_log /data/wwwlogs/access_nginx.log combined;
#    root /data/wwwroot/default;
#    index index.html index.htm index.jsp;
#    location /nginx_status {
#        stub_status on;
#        access_log off;
#        allow 127.0.0.1;
#        deny all;
#        }
#    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
#        expires 30d;
#        access_log off;
#        }
#    location ~ .*\.(js|css)?$ {
#        expires 7d;
#        access_log off;
#        }
#    location ~ {
#        proxy_pass http://127.0.0.1:8080;
#        include proxy.conf;
#        }
#    }
  

#    server{
#     listen 8087;
#    server_name _; 
#     root /home/wwwroot/default;
#     location / {
#      }  
#    }




    upstream backend{
      server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=2;
      server 139.224.209.104:8080 backup;
    }

    server{
      listen 80;
      listen 443 ssl;
      server_name wmcenter.xy-asia.com;
      #ssl on;
      default_type 'text/html';
      charset utf-8;
      ssl_certificate   /usr/local/cert/1634560_wmcenter.xy-asia.com.pem;
      ssl_certificate_key  /usr/local/cert/1634560_wmcenter.xy-asia.com.key;
      ssl_session_timeout 5m;
      ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
      ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
      ssl_prefer_server_ciphers on;
      root html;
      index index.html index.htm;
      location ~ .*\.(js|css)?$
      {
         expires 1h;
         proxy_pass   http://backend;
      }
      location / {
        proxy_pass  http://backend;
	add_header backendIP $upstream_addr;
	#proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
        #proxy_set_header Host $host;
        #proxy_set_header X-Real-IP $remote_addr;
        #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        #proxy_set_header X-Forwarded-Proto https;
        #proxy_redirect http://http://127.0.0.1:8080 $scheme://127.0.0.1:8080;
        #port_in_redirect on;
        #proxy_redirect     off;
        include proxy.conf;
      }
      #error_log /home/wwwlogs/xywm.log;
    }

     #緩存配置
    proxy_temp_file_write_size 264k;
    proxy_temp_path /data/wwwlogs/nginx_temp;
    proxy_cache_path /data/wwwlogs/nginx_cache levels=1:2 keys_zone=prc:200m inactive=5d max_size=400m;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; 


########################## vhost #############################
    include vhost/*.conf;
}
相關文章
相關標籤/搜索