nginx服务器有啥作用以及啥是反向代理

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx服务器有啥作用以及啥是反向代理相关的知识,希望对你有一定的参考价值。

参考技术A

一:nginx作为正向代理服务器:

1.正向代理:代理(proxy)服务也可以称为是正向代理,指的是将服务器部署在公司的网关,代理公司内部员工上外网的请求,可以起到一定的安全作用和管理限制作用,正向代理不支持从外网向内网访问资源,一般很少用,经本人测试,效果也不好,有很多页面打不开,在百度搜索的页面也无法返回。

server
server_name localhost;
resolver 202.106.0.20 8.8.8.8; #只能有一个resolve,但是可以用空格隔开,继续写下一个
resolver_timeout 5s;
listen  8080;
location /
proxy_pass   $scheme://$http_host/$request_uri;        proxy_set_header Host $http_host;

proxy_buffers 256 4k;
proxy_max_temp_file_size 0;

proxy_connect_timeout 30;

proxy_cache_valid 200 302 10m;
proxy_cache_valid 301 1h;
proxy_cache_valid any 1m;


2.错误码502 :Nginx作为代理服务器向后端转发请求过程中超时。

二:Nginx作为反向代理服务器:

反向代理:反向代理也叫reverse proxy,指的是代理外网用户的请求到内部的指定web服务器,并将数据返回给用户的一种方式,这是用的比较多的一种方式。

反向代理的常用指令:

1.proxy_pass:用来设置将请求转发给的后端服务器的主机,可以是主机名、IP地址:端口的方式,也可以代理到通过upstream设置的主机组,如下:

upstream webserver
#ip_hash;
server  192.168.0.201 weight=1 max_fails=2  fail_timeout=2;
server  192.168.0.202 weight=1 max_fails=2  fail_timeout=2;
server 127.0.0.1:9008 backup;


server
server_name  hfnginx.chinacloudapp.cn;
#access_log  logs/host.access.log  main;
location /  #静态网页在本机
root   html;
index  index.html;

location ~* ^/form  #指定目录在后端服务器
proxy_pass  http://webserver; #此处http://webserver后面不能加/,如果加了会提示语法错误
proxy_set_header X-Real-IP $remote_addr;


2:proxy_hide_header:用于nginx服务器作为反向代理的时候,在返回给客户端http响应的时候,隐藏后端服务版本(如php版本)的信息,可以设置在http/server或location块,如下:

Nginx会将上游服务器的响应转发给客户端,但默认不会转发以下HTTP头部字段:Date、Server、X-Pad和X-Accel-*。使用proxy_hide_header后可以任意地指定哪些HTTP头部字段不能被转发。例如:

proxy_hide_header Cache-Control;  
proxy_hide_header MicrosoftOfficeWebServer;

注:如果是nginx直接作为web服务器,要隐藏版本信息的话,使用fastcgi_hide_header:

3:proxy_pass_header:与proxy_hide_header功能相反,proxy_pass_header会将原来禁止转发的header设置为允许转发,可以设置在http/server或location块例如:

proxy_pass_header X-Accel-Redirect; 

4:proxy_pass_request_body:是否向后端服务器发送HTTP包体部分,可以设置在http/server或location块,如下:

proxy_pass_request_body on|off;  #默认为on

5:proxy_pass_request_headers:是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,如下:

proxy_pass_request_headers on | off; #默认为on

6:proxy_set_header:可以更改或添加客户端的请求头部信息内容,并转发之后端服务器,比如在后端服务器想要获取客户端的真实IP的时候,就要更改每一个报文的头部,如下:

proxy_set_header HOST  $remote_addr; #添加HOST到报文头部,其值为客户端的公网IP地址

7:proxy_set_body:更改nginx服务器接收到的客户端请求的请求内容,然后将修改后的请求转发给后端的服务器,用法如下:

proxy_set_body value;  #其中value为要修改的目标内容,可以是变量、文本或者变量的组合。

8:proxy_ip:Nginx 0.8.22版本及以上支持此功能,用于Nginx配置了多个基于域名或IP的主机的情况下,可以指定代理连接到特定的主机处理,就是强制将客户端请求绑定到指定的IP地址:

proxy_bind x.x.x.x;

9:proxy_connect_timeout:配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60秒,用法如下:

proxy_connect_timeout 10s; #10s为自定义nginx与后端服务器建立连接的超时时间

10:proxy_read_time:配置nginx服务器向后端服务器或服务器组发起read请求后,等待的超时时间:

proxy_read_time 10s; #默认为60秒

11:proxy_send_time; 配置nginx项后端服务器或服务器组发起write请求后,等待的超时时间:

proxy_send_time 10s; #默认为60s

12:proxy_http_version:用于设置nginx提供代理服务的HTTP协议的版本:

proxy_http_version 1.0|1.1;

13:proxy_method:设置nginx服务器请求后端服务器时使用的方法,一般为POST或者GET,客户端的请求方法将被忽略:

proxy_method GET|PUT;

14:proxy_ignore_client_abort:设置在客户端网络中断请求时,nginx服务器是否中断对被代理服务器的请求:

proxy_ignore_client_abort off|on; #默认为off,当客户端网络中断请求时,nginx服务器中断其对后端服务器的请求。

15:proxy_ignore_headers:设置nginx不处理后端服务器返回的数据中包含某些指定字段的报文,可以指定的有:”X-Accel-Redirect”, “X-Accel-Expires”, “Expires”或”Cache-Control”:

proxy_ignore_headers Expires;

16:proxy_redirect:修改后端服务器返回的响应头部中的location货refresh,与proxy_pass配合使用:

17:proxy_intercept_errors:设置nginx服务器返回客户端的错误状态,当后端服务器返回大于等于400的错误码的是,如果本功能是打开的,则nginx服务器返回自定义的错误页面(使用error page定义的),如果没有开启就将后端服务器返回的HTTP状态直接返回给客户端,默认为关闭:

proxy_intercept_errors on | off;

18:proxy_headers_hash_max_size:设置nginx保存HTTP报文头的hash表的上限,默认为512字节:

proxy_headers_hash_bucket_size 512;#申请nginx保存HTTP报文头的hash表的空间大小,默认为64个字节
proxy_headers_hash_max_size 512; #上限
server_names_hash_max_szie   512; #设置服务器名称的上限
server_namse_hash_bucket_size 512; #申请空间大小

19:proxy_headers_hash_bucket_size:#申请nginx保存HTTP报文头的hash表的空间大小,默认为64个字节

proxy_headers_hash_bucket_size 512; #默认为64字节

20:proxy_net_upstream:当使用了upstream的时候,可以定义在发生了特定的情况下将请求依次交给下一个组内的服务器处理,状态包括:

proxy_next_upstream  http_404 http_502;  //让404报错进入max_fails计数         upstream online
sticky;
server 172.28.70.161:8080 max_fails=0 fail_timeout=3s ;
server 172.28.70.163:8080  max_fails=0 fail_timeout=3s ;

check interval=3000 rise=2 fall=1 timeout=1000 type=http;
check_http_send "GET / HTTP/1.0\\r\\n\\r\\n";
check_http_expect_alive http_2xx http_3xx;


upstream backup
server 172.28.22.29:7777  max_fails=0 fail_timeout=3s;

21:proxy_ssl_session_reuse:配置是否基于SSL协议与后端服务器建立连接,如下:

proxy_ssl_session_reuse on | off;

nginx作用和配置

反向代理

客户端本来可以直接通过HTTP协议访问某网站应用服务器,网站管理员可以在中间加上一个Nginx,客户端请求Nginx,Nginx请求应用服务器,然后将结果返回给客户端,此时Nginx就是反向代理服务器。

  • 正向代理:某些情况下,代理我们用户去访问服务器,需要用户手动的设置代理服务器的ip和端口号。
  • 反向代理:是用来代理服务器的,代理我们要访问的目标服务器。
  • 代理服务器接受请求,然后将请求转发给内部网络的服务器集群化,并将从服务器上得到的结果返回给客户端,此时代理服务器对外就表现为一个服务器。

负载均衡

  • 负载均衡:多在高并发情况下需要使用。其原理就是将数据流量分摊到多个服务器执行,减轻每台服务器的压力,多台服务器(集群)共同完成工作任务,从而提高了数据的吞吐量。
  • Nginx可使用的负载均衡策略有:轮询(默认)、权重、ip_hash、url_hash(第三方)、fair(第三方)

动静分离

  • Nginx提供的动静分离是指把动态请求和静态请求分离开,合适的服务器处理相应的请求,使整个服务器系统的性能、效率更高。
  • Nginx可以根据配置对不同的请求做不同转发,这是动态分离的基础。静态请求对应的静态资源可以直接放在Nginx上做缓冲,更好的做法是放在相应的缓冲服务器上。动态请求由相应的后端服务器处理。

 nginx.conf配置文件

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


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;

    #gzip  on;

    server {
        listen       81;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        # 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;
    #    }
    #}
    server {
        listen       9001;
        server_name  localhost;

        location ~ /eduservice/ {           
             proxy_pass http://localhost:8001;
        }
        location ~ /eduOss/ {           
             proxy_pass http://localhost:8002;
        }
    }


}

 

以上是关于nginx服务器有啥作用以及啥是反向代理的主要内容,如果未能解决你的问题,请参考以下文章

nginx反向代理服务器以及负载均衡,从安装到配置

nginx的总结篇以及一台nginx代理多个资源服务

Nginx 负载均衡反向代理

测试技能扩展 | Nginx知识

Nginx 与 docker:反向代理不起作用

请教问题nginx反向代理proxy