温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

nginx反向代理,虚拟主机

发布时间:2020-03-31 02:53:32 来源:网络 阅读:324 作者:不会运维 栏目:系统运维

nginx反向代理,虚拟主机

一、配置虚拟主机
1.基于端口配置虚拟主机
1.1配置文件:

[root@localhost conf.d]# vim vhost.conf 
server {
        listen       8080 ;
        root         /usr/share/nginx/html;
        location / {
        }
}
server {
        listen       8081;
        root         /usr/share/nginx/html;
        location / {
        }
}

1.2验证:
nginx反向代理,虚拟主机
nginx反向代理,虚拟主机
nginx反向代理,虚拟主机
2.基于IP地址配置虚拟主机:
2.1配置文件:

[root@localhost conf.d]# vim vhost.conf 
server {
        listen      172.20.10.9:8080 ;
        root         /usr/share/nginx/html;
        location / {
        }
}
server {
        listen       172.20.10.11:8080;
        root         /usr/share/nginx/html;
        location / {
        }
}

2.2验证:
nginx反向代理,虚拟主机
nginx反向代理,虚拟主机nginx反向代理,虚拟主机

3.基于域名配置虚拟主机
3.1nginx配置文件:

[root@localhost conf.d]# vim vhost.conf 
server {
        listen      80;
        server_name  www.a.com;
        root      /data/a/html;
        location / {
        }
}
server {
        listen       80;
        server_name  www.b.com;
        root        /data/b/html;
        location / {
        }
}

3.2分别为虚拟主机配置root路径

[root@localhost /]# mkdir -pv /data/a/html/
[root@localhost html]# vim  /data/a/html/index.html 
www.a.com
[root@localhost /]# mkdir -pv /data/b/html/
[root@localhost html]# vim  /data/b/html/index.html 
www.b.com

3.3client端配置hosts文件
配置windows主机在C:\Windows\System32\drivers\etc下的hosts文件:
nginx反向代理,虚拟主机
(4)验证:
nginx反向代理,虚拟主机

nginx反向代理,虚拟主机
二.nginx反向代理:
1.反向代理配置参数:
1.1proxy_pass:
用来设置将客户端请求转发给的后端服务器主机,可以是主机名,IP地址:端口的方式,也可以代理到预先设置的主机群组,需要模块ngx_http_upstream_module支持

location /web {
            index index.html
            proxy_pass http://172.20.10.8:80;
             #不带斜线将访问的/web,等于访问后端服务器http://172.20.10.8/web/index.html,即后端服务器配置的站点根目录要有web目录才可以访问,这是一个追加/web到后端服务器http://servername:port/WEB/INDEX.HTML的操作
                        proxy_pass http://172.20.10.8:80/
                  #带斜线,等于访问后端服务器的http://172.20.10.8:80/index.html内容返回客户端
                        }

1.2proxy_hide_header
#用于nginx作为反向代理的时候,在返回客户端http响应的时候,隐藏后端服务器版本相应头部的信息,可以设置在http/server或location块;

location /web {
            index index.html;
            proxy_pass  http://172.20.10.8:80;
            proxy_hide_header ETag;
        }

1.3 proxy_pass_request_body on|off
#是否向后端服务器HTTP包体部分,可以设置在http/server或location块,默认即为开启
1.4 proxy_pass_request_headers on|off
#是否将客户端的请求头部转发给后端服务器,可以设置在http/server或location块,默认即为开启
1.5proxy_set_header
#可以更改或添加客户端的请求头部信息内容并转发后端服务器,比如在后端服务器想要获取客户端的真是IP的时候,就要更改每一个报头的头部,如下:
proxy_set_header X-Forward-For $proxy_add_x_forward_for;
#proxy_set_header HOST $remote_addr;
#添加HOST到报文头部,如果客户端为NAT上网那么真实为客户端的共用的公网IP地址
1.6proxy_hide_header field:
#用于隐藏后端服务器特定的响应首部,默认nginx在响应报文中不传递后端服务器的首部字段Date,Server,X-Pad,X-Accel等
1.7proxy_connect_timeout 60;
#配置nginx服务器与后端服务器尝试建立连接的超时时间,默认为60s,
proxy_connect_timeout 60s
60s为自定义nginx与后端服务器建立连接的超时时间
1.8proxy_read_time time;
#配置nginx服务器向后端服务器或服务器组发起read发起read请求后,等待的超时时间,默认为60s
proxy_send_time time;
#配置nginx后端服务器或服务器组发起write请求后,等待的超时时间,默认为60s
1.9proxy_http_version 1.0:
#用于设置nginx提供代理服务的HTTP协议的版本,默认http1.0
1.10proxy_ignore_client_abort off;
#当客户端网络中断时,nginx服务器中断其对后端服务器的请求。即如果此项设置为ON开启,则服务器忽略客户端中断并一直等着代理服务器执行返回,如果设置为off,则客户端中断后nginx也会中断客户端并立即记录日志,默认为off
1.11proxy_headers_hash_bucket_size 64;
#当配置proxy_hide_header和proxy_set_header的时候,用于设置nginx保存HTTP报文的hash表上限
proxy_headers_hash_max_size 512;
#设置proxy_headers_hash_max_size的最大可用空间
server_name_hash_bucket_size 512;
#设置server_name_hash表申请空间大小
server_names_hash_size 512;
#设置服务名称hash表上限大小
2.反向代理示例:
2.1单台web服务器

location / {
            proxy_pass  http://172.20.10.8;
        }

2.2指定location;

location /web {
            proxy_pass http://172.20.10.8:80/;
         }

注意后面的/
测试:

[root@node7 ~]# curl 172.20.10.9/web
172.20.10.8 index page

3缓存功能:
3.1proxy_cache zone | off; 默认off
#指明调用的缓存,或关闭缓存机制;Context:http, server, location
3.2proxy_cache_key string;
#缓存中用于“键”的内容,默认值:proxy_cache_key $scheme$proxy_host$request_uri;
3.3proxy_cach#定义对特定响应码的响应内容的缓存时长,定义在http{...}中
示例:

proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;e_valid [code ...] time;

3.4proxy_cache_path;
定义可用于proxy功能的缓存;Context:http

proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size
[inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time]
[manager_threshold=time] [loader_files=number] [loader_sleep=time]
[loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time]
[purger_threshold=time];

示例:在http配置定义缓存信息

proxy_cache_path /var/cache/nginx/proxy_cache #定义缓存保存路径,proxy_cache会自动创
建
levels=1:2:2 #定义缓存目录结构层次,1:2:2可以生成2^4x2^8x2^8=1048576个目录
keys_zone=proxycache:20m #指内存中缓存的大小,主要用于存放key和metadata(如:使用次数)
inactive=120s; #缓存有效时间
max_size=1g; #最大磁盘占用空间,磁盘存入文件内容的缓存空间最大值

3.5#调用缓存功能,需要定义在相应的配置段,如server{...};或者location等
proxy_cache proxycache;
proxy_cache_key $request_uri;
proxy_cache_valid 200 302 301 1h;
proxy_cache_valid any 1m;
3.6proxy_cache_use_stale;
#在被代理的后端服务器出现哪种情况下,可直接使用过期的缓存响应客户端,

proxy_cache_use_stale error | timeout | invalid_header | updating | http_500 |
http_502 | http_503 | http_504 | http_403 | http_404 | off ; #默认是off
3.7proxy_cache_methods GET | HEAD | POST ...;

#对哪些客户端请求方法对应的响应进行缓存,GET和HEAD方法总是被缓存
3.8proxy_set_header field value;

#设定发往后端主机的请求报文的请求首部的值
Context: http, server, location
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
请求报文的标准格式如下:
X-Forwarded-For: client1, proxy1, proxy2

3.9缓存配置:

proxy_cache_path /data/nginx/proxycache levels=1:1:1 keys_zone=proxycache:20m inactive=120s max_size=1g;
location /web {
            proxy_pass http://172.20.10.8:80;
            proxy_set_header clientip $remote_addr;
            proxy_cache proxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
         }

4.添加头部报文信息:
4.1nginx基于模块ngx_http_headers_module可以实现对头部报文添加指定的key与值#添加自定义首部,如下:

add_header name value [always];
add_header X-Via $server_addr;
add_header X-Cache $upstream_cache_status;
add_header X-Accel $server_name;
add_trailer name value [always];
添加自定义响应信息的尾部, 1.13.2版后支持

4.2nginx配置:

 location /web {
            proxy_pass http://172.20.10.8:80;
            proxy_set_header clientip $remote_addr;
            proxy_cache proxycache;
            proxy_cache_key $request_uri;
            proxy_cache_valid 200 302 301 1h;
            proxy_cache_valid any 1m;
            add_header X-Via $server_addr; 
            add_header X-Cache $upstream_cache_status;
            add_header X-Accel $server_name;
         }

4.3验证头部信息:
nginx反向代理,虚拟主机
5.反向代理高级应用:
Nginx可以基于ngx_http_upstream_module模块提供服务器分组
转发、权重分配、状态监测、调度算法等高级功能
5.1http_upstream_module配置参数

upstream name {
}
#自定义一组服务器,配置在http内
server address [parameters];
#配置一个后端web服务器,配置在upstream内,至少要有一个server服务器配置。
#server支持的parameters如下:
weight=number #设置权重,默认为1。
max_conns=number #给当前server设置最大活动链接数,默认为0表示没有限制。
max_fails=number #对后端服务器连续监测失败多少次就标记为不可用。
fail_timeout=time #对后端服务器的单次监测超时时间,默认为10秒。
backup #设置为备份服务器,当所有服务器不可用时将重新启用次服务器。
down #标记为down状态。
resolve #当server定义的是主机名的时候,当A记录发生变化会自动应用新IP而不用重启Nginx。
hash KEY consistent;
#基于指定key做hash计算,使用consistent参数,将使用ketama一致性hash算法,适用于后端是Cache服务
器(如varnish)时使用,consistent定义使用一致性hash运算,一致性hash基于取模运算。
所谓取模运算,就是计算两个数相除之后的余数,比如10%7=3, 7%4=3
hash $request_uri consistent; #基于用户请求的uri做hash
ip_hash;
#源地址hash调度方法,基于的客户端的remote_addr(源地址)做hash计算,以实现会话保持,
least_conn;
#最少连接调度算法,优先将客户端请求调度到当前连接最少的后端服务器

5.2反向代理配置示例--多台web服务器:

 upstream webserver {
               #  hash $request_uri consistent;  
                #   ip_hash;
                #  least_conn; 
        server 172.20.10.10:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.8:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.9:80 weight=1 fail_timeout=5s max_fails=3 backup;
    }
  location /web {
            index index.html;
            proxy_pass http://webserver/;
         }

5.3反向代理示例-客户端IP透传:
(1)nginx配置文件:

upstream webserver {
        #  hash $request_uri consistent;  
        #   ip_hash;
        #  least_conn; 
     #   server 172.20.10.10:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.8:80 weight=1 fail_timeout=5s max_fails=3;
        server 172.20.10.9:80 weight=1 fail_timeout=5s max_fails=3 backup;
    }   
location /web {
            index index.html;
            proxy_pass http://webserver/;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         }

(2)后端httpd服务器配置

[root@node6 conf]# vim httpd.conf
<IfModule log_config_module>
    #
    # The following directives define some format nicknames for use with
    # a CustomLog directive (see below).
    #
    LogFormat "%{X-Forwarded-For}i %h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

#重启apache访问web界面并验证apache日志:

[root@node6 conf]# tail -2 /var/log/httpd/access_log 
- 172.20.10.2 - - [01/Dec/2019:15:45:21 +0800] "GET /web/ HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"
- 172.20.10.2 - - [01/Dec/2019:15:45:21 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:70.0) Gecko/20100101 Firefox/70.0"
向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI