本文共 11422 字,大约阅读时间需要 38 分钟。
作用:隐藏客户端信息
例如:用户知道目标服务器地址,但由于网络限制等原因,无法直接访问。这时候需要先连接代理服务器,然后再由代理服务器访问目标服务器。作用:屏蔽内网服务器信息,负载均衡访问
例如:想访问我们的系统,我们的系统每个服务器由内网部署,如果整个内网暴露出去,容易引起攻击,为了能找到整个内网服务器集群,在它们前面前置一个服务器(这样就是反向代理),而这个服务器可以使用Nginx。随便启动一个Nginx实例,只是为了复制出配置
docker run -p 80:80 --name nginx -d nginx:1.10
将容器内的配置文件拷贝到当前目录:
注意别忘了后边的点
docker container cp nginx:/etc/nginx .
修改文件名称:
mv nginx conf
把这个conf移动到 /mydata/nginx 下
终止原容器:docker stop nginx
执行命令删除原容器:
docker rm $ContainerId
创建新的nginx,执行以下命令:
docker run -p 80:80 --name nginx
-v /mydata/nginx/html:/usr/share/nginx/html -v /mydata/nginx/logs:/var/log/nginx -v /mydata/nginx/conf:/etc/nginx -d nginx:1.10
我们的Nginx服务就算安装好了,并且将配置、和日志记录都挂载到宿主机上了
将nginx加入到开机自启动:docker update a8f2 --restart=always # Nginx
Windows系统 hosts文件位置:windows\System32\drivers\etc
在hosts文件添加域名和IP映射关系。 hosts文件配置:192.168.10.10 gulimall.com
映射关系表:
配置好域名后,然后访问 gulimall.com,可以看到已经能访问Nginx服务了:微服务
域名Nginx网关代理 让nginx帮我们进行反向代理,所有来自原gulimall.com的请求,都转到商品服务nginx目录结构
进入主配置目录:cd /mydata/nginx/conf/nginx.conf
nginx.conf->全局块:配置影响nginx全局的指令,如:用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等。
nginx.conf->events块:配置影响nginx服务器与用户的网络连接,如:每个进程的最大连接数,选取哪种事件驱动模型处理连接请求,是否允许同时接受多个网络连接,开启多个网络连接序列化等。
http块->http全局块:如upstream,错误页面,连接超时等。
http块->server块:配置虚拟主机的相关参数,一个http中可以有多个server。
http块->location:配置请求的路由;以及各种页面的处理情况。
进入配置目录:
cd /mydata/nginx/conf/conf.d
1、复制一份默认的配置文件
[root@localhost conf.d]# cp default.conf gulimall.conf
怎么样将Nginx服务和我们gulimall-product 关联起来呢?这里就需要使用代理了。
我们主机的虚拟机的网卡地址为:vboxnet0: inet 192.168.10.1,也可以通过该地址加端口号访问gulimall-product (localhost:10000) 服务。 http://localhost:10000/ http://192.168.10.1:10000/ 对比图: 在宿主机域名配置中,gulimall.com 配置的IP为:192.168.10.10,即虚拟机里边的IP,所以,配置的该域名+端口号可以访问我们虚拟机里边开启的服务,如数据库,redis, elasticsearch 服务: http://gulimall.com:5601/ 可以访问ES服务。说明:宿主机要和虚拟机之间通信,必须在同一局域网内,宿主机IP:192.168.10.1,虚拟机IP:192.168.10.10,他们两个是在同一局域网内,则两者可以通信。
修改 gulimall.conf 配置文件,使gulimall.com 的访问可以通过代理 proxy_pass 到 192.168.10.1:10000 服务。
[root@localhost conf.d]# vim gulimall.confserver { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { proxy_pass http://192.168.10.1:10000 } #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 /usr/share/nginx/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; #}}
然后重启
[root@localhost conf.d]# docker restart nginx;nginx[root@localhost conf.d]# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa8f2fcaa1ec7 nginx:1.10 "nginx -g 'daemon of…" 2 hours ago Restarting (1) 3 seconds ago nginxfbd8372062fc kibana:7.4.2 "/usr/local/bin/dumb…" 6 days ago Up 2 hours 0.0.0.0:5601->5601/tcp kibana26371b5a3a07 redis "docker-entrypoint.s…" 3 weeks ago Up 2 hours 0.0.0.0:6379->6379/tcp redis7dc90206f09a mysql:5.7 "docker-entrypoint.s…" 3 weeks ago Up 2 hours 0.0.0.0:3306->3306/tcp, 33060/tcp mysql[root@localhost conf.d]# docker logs nginx --tail 20nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10nginx: [emerg] unexpected "}" in /etc/nginx/conf.d/gulimall.conf:10
我们可以看到,修改配置文件出错了,第十行有问题。
vi 进入到配置文件,可以通过 :set number 命令显示文本行号,方便我们查找问题,上边报错原来是 proxy_pass 后边忘了加分号了,加上分号就好了。location / { proxy_pass http://192.168.10.1:10000; }
然后再重启Nginx,通过配置的域名访问gulimall商城首页项目:
可以看到通过代理设置,已经可以正常访问了。原理:让nginx帮我们进行反向代理,所有来自原gulimall.com的请求,都转到商品服务。
官网默认的负载均衡配置:
http { upstream myapp1 { server srv1.example.com; server srv2.example.com; server srv3.example.com; } server { listen 80; location / { proxy_pass http://myapp1; } }}
当访问 http://myapp1 的时候,默认会代理到上游服务器的 srvX.example.com
在上面的示例中,同一应用程序的3个实例在srv1-srv3上运行。如果未特别配置负载平衡方法,则默认为循环。所有请求都被代理到服务器组myapp1,nginx应用HTTP负载平衡来分发请求。nginx中的反向代理实现包括HTTP,HTTPS,FastCGI,uwsgi,SCGI,memcached和gRPC的负载平衡。修改 Nginx总的配置文件 nginx.conf
[root@localhost nginx]# cd conf[root@localhost conf]# ls -ltotal 32drwxr-xr-x. 2 root root 47 Aug 29 19:27 conf.d-rw-r--r--. 1 root root 1007 Aug 29 16:58 fastcgi_params-rw-r--r--. 1 root root 2837 Aug 29 16:58 koi-utf-rw-r--r--. 1 root root 2223 Aug 29 16:58 koi-win-rw-r--r--. 1 root root 3957 Aug 29 16:58 mime.typeslrwxrwxrwx. 1 root root 22 Aug 29 16:58 modules -> /usr/lib/nginx/modules-rw-r--r--. 1 root root 764 Aug 29 21:12 nginx.conf-rw-r--r--. 1 root root 636 Aug 29 16:58 scgi_params-rw-r--r--. 1 root root 664 Aug 29 16:58 uwsgi_params-rw-r--r--. 1 root root 3610 Aug 29 16:58 win-utf[root@localhost conf]# vi nginx.conf
添加上游服务器upstream配置
#load balancing upstream configuration upstream gulimall{ server 192.168.10.1:8888; # nacos gateway }
完整的nginx.conf文件:
[root@localhost conf]# cat nginx.confuser nginx;worker_processes 1;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;events { worker_connections 1024;}http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; #load balancing upstream configuration upstream gulimall{ server 192.168.10.1:8888; # nacos gateway } include /etc/nginx/conf.d/*.conf;}
gulimall.conf 代理修改为:
location / { #proxy_pass http://192.168.10.1:10000; proxy_pass http://gulimall; #use nginx.conf upstream load balancing }
完整的 gulimall.conf配置文件:
[root@localhost conf.d]# cat gulimall.confserver { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { #proxy_pass http://192.168.10.1:10000; proxy_pass http://gulimall; #use nginx.conf upstream load balancing } #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 /usr/share/nginx/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; #}}
重启nginx:
更改项目网关配置 在 gulimall-gateway/src/main/resources/application.yml 加入商城项目的路由规则:- id: gulimall_host_route uri: lb://gulimall-product predicates: - Host=**.gulimall.com filters: - RewritePath=/api/(?.*),/renren-fast/$\{segment}
完整代码:
spring: cloud: gateway: routes: - id: product_route uri: lb://gulimall-product predicates: - Path=/api/product/** filters: - RewritePath=/api/(?.*),/$\{segment} - id: third_party_route uri: lb://gulimall-third-party predicates: - Path=/api/thirdparty/** filters: - RewritePath=/api/(? .*),/$\{segment} - id: member_route uri: lb://gulimall-member predicates: - Path=/api/member/** filters: - RewritePath=/api/(? .*),/$\{segment} - id: ware_route uri: lb://gulimall-ware predicates: - Path=/api/ware/** filters: - RewritePath=/api/(? .*),/$\{segment} - id: admin_route uri: lb://renren-fast predicates: - Path=/api/** filters: - RewritePath=/api/(? .*),/renren-fast/$\{segment} - id: gulimall_host_route uri: lb://gulimall-product predicates: - Host=**.gulimall.com filters: - RewritePath=/api/(? .*),/renren-fast/$\{segment}## 前端项定义规则,都带 /api 前缀, lb 表示负载均衡到哪个注册器## http://localhost:8888/api/captcha.jpg 需要通过注册中心网关8888端口转发到renren-fast 8080端口服务## http://localhost:8080/renren-fast/captcha.jpg:## filters 路径重写 /api/ -> /renren-fast/
添加完路由规则之后,然后重启网关服务,根据域名进行访问:
使用网关负载均衡怎么不行呢?是哪里没设置好?路由规则的host没有匹配上。 Nginx代理坑: Nginx代理给网关的时候,会丢失请求的host信息,所以在代理转发的时候,需要设置请求头:proxy_set_header Host $host。 修改配置文件 /mydata/nginx/conf/conf.d/gulimall.conflocation / { #proxy_pass http://192.168.10.1:10000; proxy_set_header Host $host; # 设置请求头host proxy_pass http://gulimall; #use nginx.conf upstream load balancing }
完整的/nginx/conf/conf.d/gulimall.conf 文件:
[root@localhost conf.d]# cat gulimall.confserver { listen 80; server_name gulimall.com; #charset koi8-r; #access_log /var/log/nginx/log/host.access.log main; location / { #proxy_pass http://192.168.10.1:10000; proxy_set_header Host $host; proxy_pass http://gulimall; #use nginx.conf upstream load balancing } #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 /usr/share/nginx/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; #}}
然后重启Nginx服务,再次访问http://gulimall.com/
可以看到已经可以正常访问了转载地址:http://nonti.baihongyu.com/