Nginx配置入門(五):總結(jié)及應(yīng)用

一、總結(jié)

Nginx由少量的核心框架代碼和許多模塊組成,每個模塊都有其獨(dú)特的功能。因此,了解了每個模塊的實(shí)現(xiàn)功能及配置方法,我們就能知道Nginx可以為我們做什么了。

在上一篇文章中介紹了反向代理服務(wù)器的基本配置,至此Nginx初步使用的配置已經(jīng)基本都涉及到了。在實(shí)際應(yīng)用中,很多配置使用默認(rèn)項(xiàng)即可,有的配置基本不會使用到,還有的配置用于特定模塊下,也是十分常用的,比如gzip等。因此做了一個思維導(dǎo)圖,便于查找各種配置的含義。

https://www.processon.com/view/link/5dce155ee4b03d5b5a4272d1

至于其他模塊,可以到Nginx的Wiki網(wǎng)站(http://wiki.nginx.org/Modules)進(jìn)行查閱。

二、配置文件實(shí)際應(yīng)用

user                www www;#Nginx worker 進(jìn)程運(yùn)行的用戶及用戶組
worker_processes    4; # Nginx worker進(jìn)程個數(shù)

error_log           /opt/nginx/logs/nginx_error.log crit;#error日志的設(shè)置
pid                 /opt/nginx/logs/nginx.pid;#pid文件路徑
worker_rlimit_nofile 655350;#指定Nginx worker進(jìn)程可以打開的最大句柄描述符個數(shù)

events {
    use                 epoll;#選擇事件模型
    worker_connections  655350;#每個worker的最大連接數(shù)
}

http {
    include     mime.types;#
    default_type    application/octet-stream;#默認(rèn)MIME type
    charset             utf-8;
    server_tokens   off;#返回錯誤頁面時(shí)是否在server中注明Nginx版本

    server_names_hash_bucket_size   128;#以hash存儲server_name列表時(shí)的桶大小
    client_header_buffer_size       512k;#存儲HTTP頭部的內(nèi)存buffer大小
    large_client_header_buffers     4 512k;#存儲超大HTTP頭部的內(nèi)存buffer大小
    client_max_body_size            8m;#HTTP請求包體的最大值

    sendfile                on;#sendfile系統(tǒng)調(diào)用
    tcp_nopush              on;#開啟nginx在FreeBSD上使用TCP_NOPUSH套接字選項(xiàng)
    keepalive_timeout       60;#keepalive超時(shí)時(shí)間
    tcp_nodelay             on;#開啟nginx使用TCP_NODELAY選項(xiàng)的功能

    proxy_connect_timeout   5;#與代理服務(wù)器建立連接的超時(shí)時(shí)間
    proxy_read_timeout      60;#從代理服務(wù)器讀取響應(yīng)的超時(shí)時(shí)間
    proxy_send_timeout      5;#向代理服務(wù)器發(fā)送請求的超時(shí)時(shí)間
    proxy_buffer_size       32k;#設(shè)置用于讀取響應(yīng)第一部分的buffer大小
    proxy_buffers           4 64k;#設(shè)置用于讀取響應(yīng)的buffer數(shù)量和大小
    proxy_busy_buffers_size 128k;
    proxy_temp_file_write_size 128k;

    gzip                on;#打開響應(yīng)壓縮
    gzip_min_length     1k;#設(shè)置用于壓縮響應(yīng)的數(shù)量和大小
    gzip_buffers        4 16k;
    gzip_http_version   1.0;
    gzip_comp_level     2;
    gzip_types          text/plain application/x-javascript application/javascript  text/css application/xml image/jpeg image/gif image/png;#壓縮類型
    gzip_vary           on;

    proxy_temp_path     /opt/nginx/cache_temp;
    proxy_cache_path    /opt/nginx/cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

    include     /opt/nginx/conf/vhosts/*_vhost.conf;#嵌入其他配置文件

    log_format  access  '$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  access;
}


upstream example_server {
    server 192.168.1.1:80;
    server compass.abc.com;
}

server {
    listen      443 ssl;
    server_name abc.com;
    root   /opt/web_html/abc.com;
    index  index.html;

    ssl_certificate      /opt/soft/abc.com.crt;
    ssl_certificate_key  /opt/soft/abc.com.key;

    ssl_prefer_server_ciphers   on;

    ssl_protocols  SSLv3 TLSv1;
    ssl_ciphers  ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;

    access_log logs/abc.com_access.log;
    error_log  logs/abc.com_error.log;

    error_page  404  /view/404.html;

    location  / {
        root   /opt/soft/web_html/abc.com/dist;
        index  index.html use.html;
        try_files $uri $uri/ /index.html /user.html;
    }

    location ^~ /api/
    {
        proxy_set_header        Host  api.abc.com;
        proxy_set_header        X-Real-IP  $remote_addr;
        proxy_set_header        REMOTE-HOST $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://example_server;
    }

    location ^~ /zuul/api/
    {
        proxy_set_header        Host  compass-api.abc.com;
        proxy_set_header        X-Real-IP  $remote_addr;
        proxy_set_header        REMOTE-HOST $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://example_server;
    }
    
 }

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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