正向代理和Nginx反向代理配置介绍

来源:互联网 发布:北京科技大学网络登录 编辑:程序博客网 时间:2024/05/24 06:11

正向代理和Nginx反向代理配置介绍

* Author QiuRiMangCao 秋日芒草*


正向代理(代理对象是pc)

是一个位于客户端(pc)和原始服务器(google.com)之间的服务器

场景

pc —> vpn —> google.com

反向代理(代理对象是服务器)

是对于客户端而言它是原始服务器,客户端不需要进行任何特别的配置

场景

pc01 } { 服务器01
pc02 } —> 百度域名解析器 —> { 服务器02 [服务集群]
pc03 } { 服务器03

轻量级:能够花更少的资源,更少的cpu内存,更少的处理来实现更大的并发和更稳定的服务。

Nginx pk Apache

Nginx:轻量级,配置简单,高并发,高静态处理能力(css,js,jpg)
Apche:php支持的更多,插件多,高动态处理能力

使用场景

Nginx:用于作静态和反向代理服务器,处理前端静态请求
Apache:用于动态代理服务器,处理后端动态请求

启动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服务

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服务

PS D:\Program Files\MySystem\nginx\nginx-1.12.1> .\nginx.exe -s reload


nginx配置介绍

配置集群,server池,test和转发的地址对应proxy_pass http://test/;

upstream test {        #二个服务器,可以配置权重,不配就采用轮询        #server localhost:8080 8:80%    #给客户端ip设置成hash值,通过hash值取选定一个服务端口号来访问,一直都是访问同一个    #ip_hash;    # 提供服务1(后台Rs,可以是域名或ip)    server localhost:8080;    # 提供服务2    server localhost:8081;    # 设置服务访问权重,但不是个数    # server localhost:8080 weight=5;     # max_fails:健康检查(nginx对server的检查)的最大失败次数,超过次数表示该Rs不可用(判断服务是否好用)    # fail_timeout:失败的超时时间,默认为10s    # server localhost:8080 weight=5 max_fails=2 fail_timeout=10s;     # 热备配置,当前的RS全部不可用时自动启动(当其他的服务挂掉后,会启动backup标志的服务器来对外提供服务,常用于服务的备用)    # server localhost:8080 backup;     # 表示该服务永远不可用    # server localhost:8080 down; }

location / 访问的是端口的根目录

location / {    # 根目录为 /html    #root   html;    # 首页    #index  index.html index.htm;    #配置代理转发    proxy_pass http://test/;}

动静分离 - 配置静态访问资源

location /images/ {    # 定义别名    alias html/images/;}

访问资源路径

http://localhost/images/timg.jpg

location 用于匹配url,还存在一系列匹配规则

proxy_pass 通过nginx的location匹配转发到另一台代理服务,该指令用于转发location匹配到的url到server池子中

location /test{    # 原来的url不会改变,代理后http://1.1.1.1/test    proxy_pass http://1.1.1.1;      # 原来的url变为http://1.1.1.1/tmp    proxy_pass http://1.1.1.1/tmp;}

upstream 调度算法:

  1. rr轮询(默认)
  2. weight(权重)
  3. ip_hash
  4. fair(第三方)
  5. url_hash(第三方)
  6. least_conn
  7. consistent_hash


最后贴上完整的nginx.conf配置信息

#user  nobody;# 建议设置物理cpu-1worker_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相关,这台机器并发的处理能力为:worker_processes*worker_connections# 事件模型比较重要events {    worker_connections  1024;}# 配置tcp响应服务信息http {    include       mime.types;    # 文件响应类型    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;    # 响应超时时间    #keepalive_timeout  0;    keepalive_timeout  65;    # 重点,浏览器支持的话会节省网络传输文件(js,css)的大小,响应也就快,节省流量    #gzip  on;    #配置集群,server池,test和转发的地址对应proxy_pass http://test/;    upstream test {        #二个服务器,可以配置权重,不配就采用轮询        #server localhost:8080 8:80%        #给客户端ip设置成hash值,通过hash值取选定一个服务端口号来访问,一直都是访问同一个        #ip_hash;        server localhost:8080;        server localhost:8081;        # 设置服务访问权重        # server localhost:8080 weight=5;     }    # 标识nginx服务器响应的消息    server {        #nginx的端口为8000        listen       8000;        # 响应域名和默认域名,现在是本机的        # 可以购买域名并解析绑定域名到本ip电脑,设置如下server_name就能对外提供服务了        server_name  localhost;        #charset koi8-r;        #access_log  logs/host.access.log  main;        # location / 访问的是端口的根目录        location / {            # 根目录为 /html            #root   html;            # 首页            #index  index.html index.htm;            #配置代理转发            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;    #    }    #}}