一.安裝依賴包
每次安裝軟件都必須,默認會少幾個包,必須安裝
yum install autoconf automake zlib zlib-devel openssl openssl-devel pcre pcre-devel gcc
二.下載nginx和相關模塊
nginx:可以去官網 http://nginx.org/ 下載最新版
wget http://nginx.org/download/nginx-1.15.8.tar.gz
緩存模塊ngx_cache_purge:可以去 http://labs.frickle.com/nginx_ngx_cache_purge/ 下載最新版本
wget http://labs.frickle.com/files/ngx_cache_purge-2.3.tar.gz
#解壓 tar zxvf nginx-1.15.8.tar.gz tar zxvf ngx_cache_purge-2.2.tar.gz
三.編譯安裝
./configure --user=www --group=www \ --with-http_ssl_module \ --with-http_stub_status_module \ --with-http_gzip_static_module \ --with-mail \ --with-mail_ssl_module \
--with-stream \
--with-stream_ssl_module \ --add-module=../ngx_cache_purge-2.3 make & make install
#扁平版 ./configure --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-mail --with-mail_ssl_module --with-stream --with-stream_ssl_module --add-module=../ngx_cache_purge-2.3
安裝好之后,默認的nginx根目錄為 /usr/local/nginx
四.配置示例
nginx配置的例子很多,就不贅述,直接問度娘吧.
使用緩存的例子:
proxy_cache_path /tmp/cache keys_zone=tmpcache:10m; location / { proxy_pass http://127.0.0.1:8000; proxy_cache tmpcache; proxy_cache_key $uri$is_args$args; proxy_cache_purge PURGE from 127.0.0.1; }
服務器配置
user www www; worker_processes auto; events { use epoll; worker_connections 65535; multi_accept on; } http { include mime.types; default_type application/octet-stream; server_tokens off; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 600; client_header_timeout 300; client_body_timeout 300; reset_timedout_connection on; send_timeout 300; gzip on; gzip_min_length 2k; gzip_buffers 4 16k; gzip_http_version 1.0; gzip_comp_level 3; gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x-httpd-php; gzip_vary on; gzip_proxied expired no-cache no-store private auth; gzip_disable "MSIE [1-6]\."; access_log off; include /usr/local/nginx/conf/vhost/*.conf; }
然后在 vhost下所有.conf的,就都是配置文件了
標准http配置
server { listen 80; server_name 127.0.0.1 10.10.255.101 default; charset utf-8; #root /u02/www/html/iot; index index.html index.htm; location ^~ /res/ { alias /u02/www/html/res/; index index.html; #return 601; } location / { root /u02/www/html/iot; } access_log /u02/log/nginx/default.access.log; error_log /u02/log/nginx/default.error.log; }
alias / root 區別
location /img/ { alias /var/www/image/; } #若按照上述配置的話,則訪問/img/目錄里面的文件時,ningx會自動去/var/www/image/目錄找文件
location /img/ { root /var/www/image; } #若按照這種配置的話,則訪問/img/目錄下的文件時,nginx會去/var/www/image/img/目錄下找文件
所以配置額外資源,就需要使用alias , 同時,通用配置下就不能有res資源 , 應為會先被匹配訪問
五.Stream轉發配置示例
如果需要引入stream也就是tcp轉發 , 則在nginx.conf最下面增加配置
stream {
include /usr/local/nginx/conf/vhost/*.tcp; }
那么vhost下所有的.tcp,就是tcp轉發的內容
轉發配置示例:
#代理MSSQL數據庫 upstream mssql_basedata { server 192.168.2.46:1433 max_fails=3 fail_timeout=30s; } server { listen 15431; proxy_connect_timeout 1s; proxy_timeout 3s; proxy_pass mssql_basedata; }
#代理Windows遠程桌面 server { listen 12112 so_keepalive=on; proxy_connect_timeout 30s; proxy_timeout 1m; tcp_nodelay on; proxy_pass 10.100.1.11:3389; }
五.負載均衡配置示例
#第一種 權重配置法 #weight 權重 默認為1.weight越大,負載的權重就越大 #max_fails 允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤 #fail_timeout max_fails次失敗后,暫停的時間 #down down 表示單前的server暫時不參與負載 #backup 其它所有的非backup機器down或者忙的時候,請求backup機器 upstream serverlist { server 192.168.196.130 weight=1 fail_timeout=20s max_fails=1; server 192.168.196.132 weight=2 fail_timeout=20s max_fails=1; } #第二種 iphash配置法 #此方法權重配置無效 upstream serverlist { ip_hash; server 192.168.196.130 fail_timeout=20s max_fails=1; server 192.168.196.132 fail_timeout=20s max_fails=1; } #使用負載 server { listen 80; server_name xxx.com; index index.html index.htm index.php; location / { proxy_pass http://serverlist; } }
負載參考資料 :
1、輪詢(默認)
每個請求按時間順序逐一分配到不同的后端服務器,如果后端服務器down掉,能自動剔除。
upstream backserver {
server 192.168.0.14;
server 192.168.0.15;
}
2、指定權重
指定輪詢幾率,weight和訪問比率成正比,用於后端服務器性能不均的情況。
upstream backserver {
server 192.168.0.14 weight=10;
server 192.168.0.15 weight=10;
}
3、IP綁定 ip_hash
每個請求按訪問ip的hash結果分配,這樣每個訪客固定訪問一個后端服務器,可以解決session的問題。
upstream backserver {
ip_hash;
server 192.168.0.14:88;
server 192.168.0.15:80;
}
4、fair(第三方)
按后端服務器的響應時間來分配請求,響應時間短的優先分配。
upstream backserver {
server server1;
server server2;
fair;
}
5、url_hash(第三方)
按訪問url的hash結果來分配請求,使每個url定向到同一個后端服務器,后端服務器為緩存時比較有效。
upstream backserver {
server squid1:3128;
server squid2:3128;
hash $request_uri;
hash_method crc32;
}
在需要使用負載均衡的server中增加
proxy_pass http://backserver/;
upstream backserver{
ip_hash;
server 127.0.0.1:9090 down; (down 表示單前的server暫時不參與負載)
server 127.0.0.1:8080 weight=2; (weight 默認為1.weight越大,負載的權重就越大)
server 127.0.0.1:6060;
server 127.0.0.1:7070 backup; (其它所有的非backup機器down或者忙的時候,請求backup機器)
}
max_fails :允許請求失敗的次數默認為1.當超過最大次數時,返回proxy_next_upstream 模塊定義的錯誤
fail_timeout:max_fails次失敗后,暫停的時間
=====
Nginx 正向代理 :
https://www.cnblogs.com/hei-ma/p/9722478.html