Linux-nginx.conf配置文件模板


user  nginx;    #修改nginx服務的運行用戶為nginx,此用戶提前在系統創建
worker_processes  4;    #nginx運行進程的個數設置為4
worker_cpu_affinity    0001    0010    0100    1000;    #設置CPU親和力,此處為四核四線程

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

worker_rlimit_nofile    102400;        #最多可打開的文件數,為102400個

events {
    use epoll;        #使用異步IO模型
    worker_connections  102400;        #單個進程允許客戶端最大連接並發數為102400個
}

http {
    include       mime.types;    #包含的媒體類型
    default_type  application/octet-stream;    #默認的媒體類型

    #控制單個IP或者域名的訪問次數
    limit_conn_zone $binary_remote_addr zone=addr:1m;    #下接server{}的第86行

    #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;        #必須在sendfile開啟模式才有效,防止網絡堵塞,減少網絡報文段的數量

    #連接時間調優:
    #keepalive_timeout  0;
    keepalive_timeout  65;    #連接超時時間
    tcp_nodelay on;        #防止網絡阻塞,需要包含在keepalived參數中才有效
    client_header_timeout 15;    #客戶端請求頭讀取時間
    client_body_timeout 15;    #客戶端請求主體時間
    send_timeout 15;        #響應客戶端超時時間
    client_max_body_size 10m;        #設置文件上傳的大小限制為10m

    #gzip調優:
    gzip  on;        #開啟壓縮功能
    gzip_min_length  1k;    #設置允許壓縮的頁面最小字節數
    gzip_buffers     4 32k;    #壓縮緩沖區大小
    gzip_http_version 1.1;    #壓縮版本,默認為1.1
    gzip_comp_level 9;        #壓縮比例,用來指定GZIP壓縮比,1壓縮比最小,處理速度最快,9壓縮比最大,傳輸速度快,但是處理慢,也比較消耗CPU資源。
    gzip_types  text/css text/xml application/javascript;    #用來指定壓縮的類型
    gzip_vary on;        #vary header支持,該選項可以讓前端的緩存服務器緩存經過GZIP壓縮的頁面


    fastcgi_connect_timeout 300;    #指定鏈接到后端FastCGI的超時時間
    fastcgi_send_timeout 300;        #向FastCGI傳送請求的超時時間
    fastcgi_read_timeout 300;        #指定接收FastCGI應答的超時時間
    fastcgi_buffer_size 64k;        #指定讀取FastCGI應答第一部分需要用多大的緩沖區
    fastcgi_buffers 4 64k;        #指定本地需要多少和多大的緩沖區來緩沖FastCGI的應答請求
    fastcgi_busy_buffers_size 128k;    #建議設置為fastcgi_buffer的兩倍,繁忙時候的buffer
    fastcgi_temp_file_write_size 128k;        #在寫入fastcgi_temp_path時將用多大的數據庫
    #fastcgi_temp_path /data/ngx_fcgi_tmp;    
    fastcgi_cache_path /data/ngx_fcgi_cache   levels=2:2   #緩存路徑,levels目錄層次2級
    keys_zone=ngx_fcgi_cache:512m          #定義了一個存儲區域名字,緩存大小
    inactive=1d max_size=40g;          #不活動的數據在緩存中多長時間,目錄總大小

    server {
    #IP和301優化:
    #使客戶端無法使用IP地址訪問網站:
        listen       80    default_server;        #監聽端口為80
        server_name  www.bdqn.com bdqn.com;        #域名設置
                if ($host = 'bdqn.com' ) {
                        rewrite ^/(.*)$ http://www.bdqn.com/$1 permanent;
                        }    #將對bdqn.com的訪問跳轉到www.bdqn.com

    #方法一:跳轉到其他站點
    #rewrite ^ http://www.baidu.com$request_uri?;    #使用IP訪問時跳轉到www.baidu.com
    #方法二:403反饋:
        #return 403;

        charset utf-8;        #指定網站的編碼集為utf-8

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        limit_conn addr 1;        #上接http{}的第23行
    }

    #內部身份驗證:
        location /ccc/ {
                auth_basic "Welcome to Road to Death";    #提示信息
                auth_basic_user_file /usr/local/nginx/conf/passwd;    #賬號密碼文件
        }


    #FastCGI調優:
        location ~ .*\.(php|php5)?$
        {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi.conf;
        fastcgi_cache ngx_fcgi_cache;
        fastcgi_cache_valid 200 302 1h;
        fastcgi_cache_valid 301 1d;
        fastcgi_cache_valid any 1m;
        fastcgi_cache_min_uses 1;
        fastcgi_cache_use_stale error timeout invalid_header http_500;
        fastcgi_cache_key http://$host$request_uri;
        }

    #expires緩存調優:
    #匹配以.gif|jpg|jpeg|png|bmp|swf結尾的文件,緩存10年,以.js|css結尾的文件緩存30天
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
              {
              expires      3650d;
              }
        location ~ .*\.(js|css)?$
              {
              expires      30d;
              }
    #對目錄進行判斷
    #對images|javascript|js|css|flash|media|static等目錄下的內存緩存360天,對文件robot.txt緩存7天
        location ~ ^/(images|javascript|js|css|flash|media|static)/ {
              expires 360d;
              }
        location ~(robots.txt) {
              expires 7d;
              break;
              }

    #目錄文件訪問控制
    #禁止訪問images目錄下的可執行文件程序文件:
        location ~ ^/images/.*\.(php|php5|.sh|.py|.pl)$ {
                    deny all;
                }
    #多目錄組合配置方法:
    #拒絕訪問images目錄下的attachment和avatar目錄下的所有可執行文件:
     location ~ ^/images/(attachment|avatar)/.*\.(php|php5|.sh|.py|.py)$ {
                deny all;
                }
    #禁止訪問*.txt文件
#    location ~* \.(txt|doc)$ {
#                if ( -f $request_filename) {
#            root /usr/local/nginx/html;
#            break;
#               }
#        deny all;
#    }

    #重定向到某個URL
        location ~* \.(txt|doc)$ {
                if ( -f $request_filename) {
                root /usr/local/nginx/html;
                rewrite ^/(.*)$ http://www.baidu.com last;    #對應的URL路徑
                break;
            }
        }

    #對指定目錄進行限制:
        location /aaa/       { return 404 ; }
        location /bbb/       { return 403 ; }
    #上述等同於:
        location ~ ^/(aaa|bbb)/ {
            deny all;
        }

    #來源訪問控制:
    #允許192.168.1.0/24網站下的aaa目錄,拒絕所有
        location ~ ^/(aaa)/ {
            allow 192.168.1.0/24;
            deny all;
        }
    #允許192.168.1.0/24網段訪問網站,拒絕所有,針對整個網站如下:
    #location / {
        #    allow 192.168.1.0/24;    #此處可使用精確匹配,也可使用正則
        #    deny all;
        #}
    #也可以通過if語句控制,給以友好的錯誤提示:
        if ( $remote_addr = 192.168.1.38 ) {
            return 404;
        }

        #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;
    #    }
    #}

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM