Nginx配置文件

作者:劉賓, thomas_liub@hotmail.com
請尊重作者著作權,轉載請注明出處,謝謝!


例子

worker_processes  4;            # 工作進程數量, 建議配置成主機CPU內核數量
error_log logs/error.log debug; # 日志文件和日志級別, debug, info, error,
pid logs/nginx.pid;             # nginx 進程pid文件

events {
    #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,如果跑在FreeBSD上面,就用kqueue模型。
    use epoll;
    worker_connections 1024;    # 單工作進程支持最大連接數
}

# 一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,但是nginx分配請求并不均勻,所以建議與ulimit -n的值保持一致。
# worker_rlimit_nofile 65535;

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 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,
    #對于普通應用,必須設為 on,
    #如果用來進行下載等應用磁盤IO重負載應用,可設置為 off,
    #以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
    sendfile     on;
    #tcp_nopush     on;
    #tcp_nodelay     on;
    keepalive_timeout  65;

    #開啟gzip壓縮
    gzip  on;
    gzip_disable "MSIE [1-6].";

    # lua擴展模塊包路徑
    lua_package_path '$prefix/lua/?.lua;$prefix/lua/lib/?.lua;;';

    # lua cache,開發模式下設為off, 發布模式下設為on
    lua_code_cache off;

    # for open HTTPS connection
    ssl on;
    ssl_certificate /home/xxx/project/resty-gateway/luamod/conf/server.crt;
    ssl_certificate_key /home/xxx/project/resty-gateway/luamod/conf/server.key;

    resolver 192.168.32.21;   # DNS server

    #設定請求緩沖
    client_header_buffer_size    128k;
    large_client_header_buffers  4 128k;

    # 設定負載均衡后臺服務器列表
    upstream backend {
    #ip_hash;
    server 192.168.10.100:8080 max_fails=2 fail_timeout=30s ;
    server 192.168.10.101:8080 max_fails=2 fail_timeout=30s ;
    }

    # SSO server
    server {
        listen 8888;
        server_name  localhost;
        
        #定義服務器的默認網站根目錄位置
        root html;

        #設定本虛擬主機的訪問日志
        access_log  logs/nginx.access.log  main;

        # bypass all to SSO server.
        location / {
            default_type text/html;
            proxy_pass http://192.168.32.28:9090;
        }
    }

    # application server
    server {
        listen 9999;
        server_name  localhost;
       
        root /home/xxx/dnc/mdc/static;

        set $session_storage redis;
        set $session_redis_host 192.168.32.30;

        # url rules
        location /api/dnc {
            # lua extend modules
            access_by_lua_file  lua/sso/access_resty.lua;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_pass http://192.168.32.30:8090;
        }

        location /api/mdc {
            # lua extend modules
            access_by_lua_file  lua/sso/access_resty.lua;
            proxy_pass http://192.168.32.30:6091;
        }
        
        location /NginxStatus {
            stub_status on;
            access_log on;
            auth_basic "NginxStatus";
            auth_basic_user_file conf/htpasswd;
            #htpasswd文件的內容可以用apache提供的htpasswd工具來產生。
        }

        # 靜態文件,nginx自己處理
        location ~ ^/(images|javascript|flash|media|static|php)/ {
            #過期30天,靜態文件不怎么更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
            root /home/xxx/dnc/mdc/;
            expires 10d;
        }
        
        # 靜態文件,css/js
        location ~ .*.(js|css)?${ 
            expires 1h; 
        }

        location / {
            default_type text/html;
            access_by_lua_file  lua/sso/access_resty.lua;
            #定義首頁索引文件的名稱
            index index.php index.html index.htm;
        }

        # 定義錯誤提示頁面
        error_page   500 502 503 504 /50x.html;
        location = /50x.html {
        }

        #禁止訪問 .htxxx 文件
            location ~ /.ht {
            deny all;
        }
    }
}

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容