博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx+Windows搭建域名访问环境
阅读量:4147 次
发布时间:2019-05-25

本文共 11422 字,大约阅读时间需要 38 分钟。

文章目录

一、正向代理

作用:隐藏客户端信息

例如:用户知道目标服务器地址,但由于网络限制等原因,无法直接访问。这时候需要先连接代理服务器,然后再由代理服务器访问目标服务器。

二、反向代理

作用:屏蔽内网服务器信息,负载均衡访问

例如:想访问我们的系统,我们的系统每个服务器由内网部署,如果整个内网暴露出去,容易引起攻击,为了能找到整个内网服务器集群,在它们前面前置一个服务器(这样就是反向代理),而这个服务器可以使用Nginx。

三、Docker安装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配置

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:配置请求的路由;以及各种页面的处理情况。

1、反向代理配置

进入配置目录:

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的请求,都转到商品服务。

2、Nginx负载均衡

在这里插入图片描述

上一节我们是让Nginx直接代理到我们的商品服务:192.168.10.1:10000,这样会有问题,如果商品服务做调整或者增加其他的微服务,则需要直接修改Nginx的配置文件,商品服务上线或下线我们也很难操作,这时候就需要网关来处理了,Nginx先将请求负载到网关,然后再由网关转发到 微服务。

官网默认的负载均衡配置:

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.conf

location / {       #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/

你可能感兴趣的文章
阿里面经最新
查看>>
Spring AOP四种实现方式
查看>>
ThreadPoolExecutor使用和思考(上)-线程池大小设置与BlockingQueue的三种实现区别
查看>>
Maven搭建SSH的pom的代码详解(转载)
查看>>
SSH Maven pom.xml 备忘
查看>>
推荐一位大佬博客<关于记录淘淘商城开发点点滴滴>
查看>>
redis集群原理
查看>>
Redis五种数据类型介绍
查看>>
git常用命令
查看>>
[IDEA]习惯用的idea快捷方法
查看>>
【SpringMVC整合MyBatis】springmvc异常处理-全局异常处理器开发
查看>>
spring cloud的的服务消费者
查看>>
特征脸技术及其在人像检测应用研究
查看>>
spring mvc 的jpa JpaRepository数据层 访问方式汇总
查看>>
Lombok用法
查看>>
Windows下用Nginx配置https服务器
查看>>
spring quartz 的定时器cronExpression表达式写法(转载)
查看>>
环境安装准备
查看>>
Freemaker使用记录
查看>>
1分钟通晓C语言的4种预定义符号
查看>>