nginx配置文件nginx.conf簡單介紹


一、Nginx的配置文件結構

    Nginx的配置文件nginx.conf位於其安裝目錄的conf目錄下,nginx.conf由多個塊組成,最外面的塊是main,main包含Events

和HTTP,HTTP包含upstream和多個Server,Server又包含多個location。其中main(全局設置)、server(主機設置)、

upstream(負載均衡服務器設置)和 location(URL匹配特定位置的設置)。

  • main塊設置的指令將影響其他所有設置;
  • server塊的指令主要用於指定主機和端口;
  • upstream指令主要用於負載均衡,設置一系列的后端服務器;
  • location塊用於匹配網頁位置。
#user指定Nginx Worker進程運行用戶以及用戶組,默認由nobody賬號運行。
#user  nobody;

#worker_processes指定了Nginx要開啟的進程數。每個Nginx進程平均耗費10M~12M內存。建議指定和CPU的數量一致即可。
worker_processes  1;

#error_log用來定義全局錯誤日志文件。日志輸出級別有debug、info、notice、warn、error、crit可供選擇,
#其中,debug輸出日志最為最詳細,而crit輸出日志最少。
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#用來指定進程pid的存儲文件位置
#pid        logs/nginx.pid;


#events:設定Nginx的工作模式及連接數上限:
#use是個事件模塊指令,用來指定Nginx的工作模式Nginx支持的工作模式有select、poll、kqueue、epoll、rtsig和/dev/poll;
#epoll是多路復用IO(I/O Multiplexing)中的一種方式,但是僅用於linux2.6以上內核,可以大大提高nginx的性能
#其中select和poll都是標准的工作模式,kqueue和epoll是高效的工作模式,不同的是epoll用在Linux平台上,而kqueue用在BSD
#系統中。對於Linux系統,epoll工作模式是首選。

#worker_connections:用於定義Nginx每個進程的最大連接數,默認是1024;
#值得注意的是如果你不知道Nginx該使用哪種輪詢方法的話,它會選擇一個最適合你操作系統的。
#最大客戶端連接數由worker_processes和worker_connections決定,即Max_client=worker_processes*worker_connections。
#在作為反向代理時,max_clients變為:max_clients = worker_processes * worker_connections/4。
#進程的最大連接數受Linux系統進程的最大打開文件數限制,在執行操作系統命令“ulimit -n 65536”后worker_connections的設置才能生效。

#注意:關於ulimit -n 65536,請自行百度

events {
    worker_connections  1024;
}


http {
    #文件擴展名與類型映射表:實現對配置文件所包含的文件的設定,可以減少主配置文件的復雜度。類似於Apache中的include方法。
    include       mime.types;
    #這里設定默認類型為二進制流,也就是當文件類型未定義時使用這種方式,例如在沒有配置PHP環境時,Nginx是不予解析的,此時,
    #用瀏覽器訪問PHP文件就會出現下載窗口
    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"';
    
    #這是我們項目中使用的,僅供參考
    log_format  main  '$remote_addr  - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
              '"$http_user_agent" "$http_x_forwarded_for" "$host"  "$cookie_ssl_edition" '
             '"$upstream_addr"   "$upstream_status"  "$request_time"  '
             '"$upstream_response_time" ';
    
    #用來指定此虛擬主機的訪問日志存放路徑,最后的main用於指定訪問日志的輸出格式;
    #access_log  logs/access.log  main;

    #sendfile參數用於開啟高效文件傳輸模式,將tcp_nopush和tcp_nodelay兩個指令設置為on用於防止網絡阻塞;
    sendfile        on;
    #激活tcp_nopush參數可以允許把httpresponse header和文件的開始放在一個文件里發布,積極的作用是減少網絡報文段的數量;
    #tcp_nopush     on;
    
    #激活tcp_nodelay,內核會等待將更多的字節組成一個數據包,從而提高I/O性能
    #tcp_nodelay on;

    #設置客戶端連接保持活動的超時時間。在超過這個時間之后,服務器會關閉該連接,單位是"秒";
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #開啟gzip壓縮功能
    #gzip  on;

    #反向代理負載均衡配置部分
    upstream backend_server {
        #max_fails:定義的時間段內連接該主機的失敗次數,以此來斷定 fail_timeout定義的時間段內該主機是否可用。
        #默認情況下這個數值設置為 1。零值的話禁用這個數量的嘗試。設置在指定時間內連接到主機的失敗次數,超過該次數該主機被認為不可用。
        #這里是在30s內嘗試2次失敗即認為主機不可用!
        server   localhost:80 weight=1 max_fails=2 fail_timeout=30s;
    }
    
    #虛擬主機配置
    server {
        #listen用於監聽指定虛擬主機的服務端口
        listen       80;
        #server_name用於指定IP地址或者域名,多個域名之間用空格分開;
        server_name  localhost;
        
        #Charset用於設置網頁的默認編碼格式
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #ocation URL匹配配置
        location / {
            #站點根目錄,即網站程序存放目錄
            root   html;
            #首頁排序
            index  index.html index.htm;
        }

        #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;
        }
        
        #
        location /api/ {
            #代理轉發,如果在proxy_pass后面的url加/,表示絕對根路徑;如果沒有/,表示相對路徑,把匹配的路徑部分也給代理走
            proxy_pass http://10.102.46.93:18685/api/xy/;
            tcp_nodelay on;
            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 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; #限制此路徑(或者uri)訪問
        #}
    }


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

}
關於proxy_set_header,還不理解,下次有時間在好好看看;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # nginx做轉發時,添加該配置可以透傳真是IP

 


免責聲明!

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



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