nginx的安装和配置

拉取镜像

docker pull nginx

创建目录

创建一个目录用来存放文件,方便我们进行修改

mkdir -p /mydata/nginx/conf /mydata/nginx/html

创建配置文件

在我们创建的目录下创建一个配置文件

touch /mydata/nginx/conf/nginx.conf

修改配置文件

把我们创建的目录下的 nginx.conf 修改为以下内容

#工作进程数  1 ,不要超过计算机的核数,四核配置4,八核配置8
worker_processes  1;

#工作连接数,也就是线程,一个进程有1024个线程,
events {
    worker_connections  1024;
}

#http请求配置
http {
    default_type  application/octet-stream;
	
	#sendfile为发送文件,要on开启
    sendfile        on;
	
	#keepalive_timeout超时时间
    keepalive_timeout  65;
	
	server {
	    #监听的端口,这里为80
		listen       80;
		#server_name就是域名,
		server_name  localhost;
		
		#location域名代理地址
	    # / 代表所有请求路径
		location / {
			root /usr/share/nginx/html;
			index  index.html;
		}
	}
}

创建html

创建在 /mydata/nginx/html目录下创建index.html

touch /mydata/nginx/index.html

index.html内容为

<html>
<body>
<h1>index.html...</h1>
</body>
</html>

启动容器

docker run --name nginx -p 80:80 -v /mydata/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v /mydata/nginx/html:/usr/share/nginx/html --restart always -d nginx

访问

在这里插入图片描述

配置反向代理

本机反向代理

  1. 设置本机域名映射

    在:C:\Windows\System32\drivers\etc
    配置:192.168.23.138 helloworld.com
    

    本机请求:gulimall.com 会请求到虚拟机,虚拟机会监听80端口,就做到请求nginx上

  2. 设置域名

    #监听的端口,这里为80
    listen       80;
    #server_name就是域名,
    server_name  helloworld.com;
    
  3. 配置请求地址

    #location域名代理地址
    # / 代表所有请求路径
    location / {
    	proxy_pass http://192.168.23.1:10000;
    }
    

    nginx反向代理了http://192.168.23.1:10000
    (本机的ID地址)

  4. 测试

    http://helloworld.com/就请求了主页

负载均衡

以下域名配置均为:helloworld.com

  1. 先配置上游服务器

    upstream helloworld{
                server 192.168.23.1:88;
            }
    
  2. 配置请求到上游服务器

    location / {
            proxy_set_header Host $host;
            proxy_pass http://helloworld;
    }
    
    1. 结合上面的反向代理的配置,当访问域名时,本机会访问到虚拟机的nginx(正向代理)

    2. nginx会负载均衡到上游服务器上(以下都是反向代理过程)

    3. 上游服务器转交给网关

    4. 网关配置路由

      # 商品服务路由
      - id: gulimall_host_router
        uri: lb://gulimall-product
        predicates:
        - Host=helloworld.com, www.helloworld.com, item.helloworld.com
      
    5. 网关根据路由访问对应的服务

  3. 测试

    http://helloworld.com/

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千禧618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值