Nginx的安裝和配置文件

1、什麼是Nginxjavascript

  反向代理的高手,能夠作web服務器、smtp服務器、ftp服務器,也能夠作waf等等。原理,反向代理,收集client請求而後轉發給本身lan內的服務器,將請求到的資源迴轉給客戶端。因此也能夠作負載均衡等等。php

2、安裝Nginxcss

首先是Ubuntu的html

 1 #Ubuntu16.04
 2 apt-get install bulid-essential
 3 apt-get install libtool
 4 apt-get install libpcre3 libpcre3-dev
 5 apt-get install zlib1g-dev
 6 apt-get install openssl#(Ubuntu16.04免安裝)
 7 wget http://nginx.org/download/nginx-1.13.1.tar.gz
 8 tar -xzvf nginx-1.13.1.tar.gz
 9 mv nginx-1.13.1 nginx
10 mv nginx /home/username/
11 cd /home/username/nginx/
12 ./configure --user=www --group=www --prefix=/home/username/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_realip_module
13 make
14 make install

而後是Centos的前端

1 yum install nginx

講解配置文件,以CentOS舉例,配置文件/etc/nginx/nginx.conf java

 

  1 #定義Nginx運行的用戶和用戶組
  2 user www www;
  3 
  4 #nginx進程數,建議設置爲等於CPU總核心數。
  5 worker_processes 8;
  6 
  7 #全局錯誤日誌定義類型,[ debug | info | notice | warn | error | crit ]
  8 error_log /var/log/nginx/error.log info;
  9 
 10 #進程文件
 11 pid /var/run/nginx.pid;
 12 
 13 #一個nginx進程打開的最多文件描述符數目,理論值應該是最多打開文件數(系統的值ulimit -n)與nginx進程數相除,可是nginx分配請求並不均勻,因此建議與ulimit -n的值保持一致。
 14 worker_rlimit_nofile 65535;
 15 
 16 #工做模式與鏈接數上限
 17 events
 18 {
 19     #參考事件模型,use [ kqueue | rtsig | epoll | /dev/poll | select | poll ]; epoll模型是Linux 2.6以上版本內核中的高性能網絡I/O模型,若是跑在FreeBSD上面,就用kqueue模型。
 20     use epoll;
 21     #單個進程最大鏈接數(最大鏈接數=鏈接數*進程數)
 22     worker_connections 65535;
 23 }
 24 
 25 #設定http服務器
 26 http
 27 {
 28     include mime.types; #文件擴展名與文件類型映射表或者 include /etc/nginx.mime.types
 29     default_type application/octet-stream; #默認文件類型
 30     #charset utf-8; #默認編碼
 31     server_names_hash_bucket_size 128; #服務器名字的hash表大小
 32     client_header_buffer_size 32k; #上傳文件大小限制
 33     large_client_header_buffers 4 64k; #設定請求緩
 34     client_max_body_size 8m; #設定請求緩
 35     sendfile on; #開啓高效文件傳輸模式,sendfile指令指定nginx是否調用sendfile函數來輸出文件,對於普通應用設爲 on,若是用來進行下載等應用磁盤IO重負載應用,可設置爲off,以平衡磁盤與網絡I/O處理速度,下降系統的負載。注意:若是圖片顯示不正常把這個改爲off。
 36     autoindex on; #開啓目錄列表訪問,合適下載服務器,默認關閉。
 37     tcp_nopush on; #防止網絡阻塞
 38     tcp_nodelay on; #防止網絡阻塞
 39     keepalive_timeout 120; #長鏈接超時時間,單位是秒
40 types_hash_max_size 2048;
41 42 #FastCGI相關參數是爲了改善網站的性能:減小資源佔用,提升訪問速度。下面參數看字面意思都能理解。 43 fastcgi_connect_timeout 300; 44 fastcgi_send_timeout 300; 45 fastcgi_read_timeout 300; 46 fastcgi_buffer_size 64k; 47 fastcgi_buffers 4 64k; 48 fastcgi_busy_buffers_size 128k; 49 fastcgi_temp_file_write_size 128k; 50 51 #gzip模塊設置 52 gzip on; #開啓gzip壓縮輸出 53 gzip_min_length 1k; #最小壓縮文件大小 54 gzip_buffers 4 16k; #壓縮緩衝區 55 gzip_http_version 1.0; #壓縮版本(默認1.1,前端若是是squid2.5請使用1.0) 56 gzip_comp_level 2; #壓縮等級 57 gzip_types text/plain application/x-javascript text/css application/xml; 58 #壓縮類型,默認就已經包含text/html,因此下面就不用再寫了,寫上去也不會有問題,可是會有一個warn。 59 gzip_vary on; 60 #limit_zone crawler $binary_remote_addr 10m; #開啓限制IP鏈接數的時候須要使用 61 62 upstream blog.ha97.com { 63 #upstream的負載均衡,weight是權重,能夠根據機器配置定義權重。weigth參數表示權值,權值越高被分配到的概率越大。 64 server 192.168.80.121:80 weight=3; 65 server 192.168.80.122:80 weight=2; 66 server 192.168.80.123:80 weight=3; 67 } 68
69 include /etc/nginx/conf.d/*.conf;
70 71 #虛擬主機的配置 72 server 73 { 74 #監聽端口,如果IPv6 就是 listen [::]:80 75 listen 80; 76 #域名能夠有多個,用空格隔開 77 server_name www.ha97.com ha97.com; 78 index index.html index.htm index.php; 79 root /data/www/ha97; 80 location ~ .*\.(php|php5)?$ 81 { 82 fastcgi_pass 127.0.0.1:9000; 83 fastcgi_index index.php; 84 include fastcgi.conf; 85 } 86 #圖片緩存時間設置 87 location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ 88 { 89 expires 10d; 90 } 91 #JS和CSS緩存時間設置 92 location ~ .*\.(js|css)?$ 93 { 94 expires 1h; 95 } 96 #日誌格式設定 97 log_format access '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" $http_x_forwarded_for'; 98 #定義本虛擬主機的訪問日誌 99 access_log /var/log/nginx/ha97access.log access; 100 101 #對 "/" 啓用反向代理 102 location / { 103 proxy_pass http://127.0.0.1:88; 104 proxy_redirect off; 105 proxy_set_header X-Real-IP $remote_addr; 106 #後端的Web服務器能夠經過X-Forwarded-For獲取用戶真實IP 107 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 108 #如下是一些反向代理的配置,可選。 109 proxy_set_header Host $host; 110 client_max_body_size 10m; #容許客戶端請求的最大單文件字節數 111 client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數, 112 proxy_connect_timeout 90; #nginx跟後端服務器鏈接超時時間(代理鏈接超時) 113 proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時) 114 proxy_read_timeout 90; #鏈接成功後,後端服務器響應時間(代理接收超時) 115 proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小 116 proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k如下的設置 117 proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2) 118 proxy_temp_file_write_size 64k; 119 #設定緩存文件夾大小,大於這個值,將從upstream服務器傳 120 } 121 122 #設定查看Nginx狀態的地址 123 location /NginxStatus { 124 stub_status on; 125 access_log on; 126 auth_basic "NginxStatus"; 127 auth_basic_user_file conf/htpasswd; 128 #htpasswd文件的內容能夠用apache提供的htpasswd工具來產生。 129 } 130 131 #本地動靜分離反向代理配置 132 #全部jsp的頁面均交由tomcat或resin處理 133 location ~ .(jsp|jspx|do)?$ { 134 proxy_set_header Host $host; 135 proxy_set_header X-Real-IP $remote_addr; 136 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 137 proxy_pass http://127.0.0.1:8080; 138 } 139 #全部靜態文件由nginx直接讀取不通過tomcat或resin 140 location ~ .*.(htm|html|gif|jpg|jpeg|png|bmp|swf|ioc|rar|zip|txt|flv|mid|doc|ppt|pdf|xls|mp3|wma)$ 141 { expires 15d; } 142 location ~ .*.(js|css)?$ 143 { expires 1h; }
144 error_page 404/404.html;location = /40x.html{} #也能夠error_page 500 502 503 504 /50x.html;location= /50x.html{}
145 } 146 }

TLS的server配置node

 1 # Settings for a TLS enabled server.
 2 #
 3 #    server {
 4 #        listen       443 ssl http2 default_server;
 5 #        listen       [::]:443 ssl http2 default_server;
 6 #        server_name  _;
 7 #        root         /usr/share/nginx/html;
 8 #
 9 #        ssl_certificate "/etc/pki/nginx/server.crt";
10 #        ssl_certificate_key "/etc/pki/nginx/private/server.key";
11 #        ssl_session_cache shared:SSL:1m;
12 #        ssl_session_timeout  10m;
13 #        ssl_ciphers HIGH:!aNULL:!MD5;
14 #        ssl_prefer_server_ciphers on;
15 #
16 #        # Load configuration files for the default server block.
17 #        include /etc/nginx/default.d/*.conf;
18 #
19 #        location / {
20 #        }
21 #
22 #        error_page 404 /404.html;
23 #            location = /40x.html {
24 #        }
25 #
26 #        error_page 500 502 503 504 /50x.html;
27 #            location = /50x.html {
28 #        }
29 #    }

 

3、參考文獻:nginx

http://www.ha97.com/5194.htmlweb

相關文章
相關標籤/搜索