正向代理和Nginx反向代理配置介绍
正向代理和Nginx反向代理配置介紹
* Author QiuRiMangCao 秋日芒草*
正向代理(代理對象是pc)
是一個位于客戶端(pc)和原始服務(wù)器(google.com)之間的服務(wù)器
場景
pc —> vpn —> google.com
反向代理(代理對象是服務(wù)器)
是對于客戶端而言它是原始服務(wù)器,客戶端不需要進行任何特別的配置
場景
pc01 } { 服務(wù)器01
pc02 } —> 百度域名解析器 —> { 服務(wù)器02 [服務(wù)集群]
pc03 } { 服務(wù)器03
輕量級:能夠花更少的資源,更少的cpu內(nèi)存,更少的處理來實現(xiàn)更大的并發(fā)和更穩(wěn)定的服務(wù)。
Nginx pk Apache
Nginx:輕量級,配置簡單,高并發(fā),高靜態(tài)處理能力(css,js,jpg)
Apche:php支持的更多,插件多,高動態(tài)處理能力
使用場景
Nginx:用于作靜態(tài)和反向代理服務(wù)器,處理前端靜態(tài)請求
Apache:用于動態(tài)代理服務(wù)器,處理后端動態(tài)請求
啟動nginx
PS D:\Program Files\MySystem\nginx-1.12.1> start nginx
查看nginx線程
PS D:\Program Files\MySystem\nginx-1.12.1> ps nginx
d—– 2017/9/23 12:38 temp
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI Proces
sName
——- —— —– —– —— – – ——
194 12 2112 9076 0.08 5112 8 nginx
188 14 21260 27512 0.06 7424 8 nginx
188 14 21260 27548 0.08 8108 8 nginx
188 14 21256 27516 0.08 9228 8 nginx
188 14 21252 27552 0.13 9904 8 nginx
停止nginx服務(wù)
PS D:\Program Files\MySystem\nginx-1.12.1> .\nginx.exe -s quit
檢查nginx配置文件是否正確
PS D:\Program Files\MySystem\nginx\nginx-1.12.1> .\nginx.exe -t
nginx: the configuration file D:\Program Files\MySystem\nginx\nginx-1.12.1/conf/nginx.conf syntax is ok
nginx: configuration file D:\Program Files\MySystem\nginx\nginx-1.12.1/conf/nginx.conf test is successful
修改配置文件后重啟nginx服務(wù)
PS D:\Program Files\MySystem\nginx\nginx-1.12.1> .\nginx.exe -s reload
nginx配置介紹
配置集群,server池,test和轉(zhuǎn)發(fā)的地址對應(yīng)proxy_pass http://test/;
upstream test {#二個服務(wù)器,可以配置權(quán)重,不配就采用輪詢#server localhost:8080 8:80%#給客戶端ip設(shè)置成hash值,通過hash值取選定一個服務(wù)端口號來訪問,一直都是訪問同一個#ip_hash;# 提供服務(wù)1(后臺Rs,可以是域名或ip)server localhost:8080;# 提供服務(wù)2server localhost:8081;# 設(shè)置服務(wù)訪問權(quán)重,但不是個數(shù)# server localhost:8080 weight=5; # max_fails:健康檢查(nginx對server的檢查)的最大失敗次數(shù),超過次數(shù)表示該Rs不可用(判斷服務(wù)是否好用)# fail_timeout:失敗的超時時間,默認為10s# server localhost:8080 weight=5 max_fails=2 fail_timeout=10s; # 熱備配置,當前的RS全部不可用時自動啟動(當其他的服務(wù)掛掉后,會啟動backup標志的服務(wù)器來對外提供服務(wù),常用于服務(wù)的備用)# server localhost:8080 backup; # 表示該服務(wù)永遠不可用# server localhost:8080 down; }location / 訪問的是端口的根目錄
location / {# 根目錄為 /html#root html;# 首頁#index index.html index.htm;#配置代理轉(zhuǎn)發(fā)proxy_pass http://test/; }動靜分離 - 配置靜態(tài)訪問資源
location /images/ {# 定義別名alias html/images/; }訪問資源路徑
http://localhost/images/timg.jpg
location 用于匹配url,還存在一系列匹配規(guī)則
proxy_pass 通過nginx的location匹配轉(zhuǎn)發(fā)到另一臺代理服務(wù),該指令用于轉(zhuǎn)發(fā)location匹配到的url到server池子中
location /test{# 原來的url不會改變,代理后http://1.1.1.1/testproxy_pass http://1.1.1.1; # 原來的url變?yōu)閔ttp://1.1.1.1/tmpproxy_pass http://1.1.1.1/tmp; }upstream 調(diào)度算法:
最后貼上完整的nginx.conf配置信息
#user nobody; # 建議設(shè)置物理cpu-1 worker_processes 1;#error_log logs/error.log; # 日志配置 日志文件地址 日志級別 #error_log logs/error.log notice; #error_log logs/error.log info;# 進程存放目錄地址 #pid logs/nginx.pid;# 事件模型和worker_processes相關(guān),這臺機器并發(fā)的處理能力為:worker_processes*worker_connections # 事件模型比較重要 events {worker_connections 1024; }# 配置tcp響應(yīng)服務(wù)信息 http {include mime.types;# 文件響應(yīng)類型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"';#access_log logs/access.log main;sendfile on;#tcp_nopush on;# 響應(yīng)超時時間#keepalive_timeout 0;keepalive_timeout 65;# 重點,瀏覽器支持的話會節(jié)省網(wǎng)絡(luò)傳輸文件(js,css)的大小,響應(yīng)也就快,節(jié)省流量#gzip on;#配置集群,server池,test和轉(zhuǎn)發(fā)的地址對應(yīng)proxy_pass http://test/;upstream test {#二個服務(wù)器,可以配置權(quán)重,不配就采用輪詢#server localhost:8080 8:80%#給客戶端ip設(shè)置成hash值,通過hash值取選定一個服務(wù)端口號來訪問,一直都是訪問同一個#ip_hash;server localhost:8080;server localhost:8081;# 設(shè)置服務(wù)訪問權(quán)重# server localhost:8080 weight=5; }# 標識nginx服務(wù)器響應(yīng)的消息server {#nginx的端口為8000listen 8000;# 響應(yīng)域名和默認域名,現(xiàn)在是本機的# 可以購買域名并解析綁定域名到本ip電腦,設(shè)置如下server_name就能對外提供服務(wù)了server_name localhost;#charset koi8-r;#access_log logs/host.access.log main;# location / 訪問的是端口的根目錄location / {# 根目錄為 /html#root html;# 首頁#index index.html index.htm;#配置代理轉(zhuǎn)發(fā)proxy_pass http://test/;}# 錯誤頁面 標志 訪問頁面#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;# }#}}總結(jié)
以上是生活随笔為你收集整理的正向代理和Nginx反向代理配置介绍的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蒙特卡罗方法入门
- 下一篇: nginx 正向代理(上网代理) 反向代