Nginx入门——Nginx的docker版本和windows版本安装和使用 & 代理的概念 & 负载分配策略_docker nginx

img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上软件测试知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取


1.Nginx反向代理,代理的概念,正向代理,反向代理;
2.docker版本的nginx配置,挂载启动,开放端口;
3.windows版本的使用,代理多个端口的方式;
4.负载分配的策略,轮询,权重,ip_hash方式;

在这里插入图片描述

nginx是啥

http://nginx.org/en/download.html

在这里插入图片描述

Nginx(engine x)是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。Nginx是由伊戈尔寒索耶夫为俄罗访问量第二的Rambler.ru站点(俄文:PaM6nep)开发的,公开版本1.19.6发布于2020年12月15日。

rambler.ru

在这里插入图片描述

其将源代码以类[BSD许可证正的形式发布,因它的稳定性、丰富的功能集、简单的配置文件和低系统资源的消耗而闻名。Nginx是一款轻量级的Wb服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器,在BSD-ike协议下发行。其特点是占有内存少,并发能力强,事实上ngix的并发能力在同类型的网页服务器中表现较好。

正向代理和反向代理

正向代理

正向代理时,由客户端发送对某一个目标服务器的请求,代理服务器在中间将请求转发给该目标服务器,目标服务器将结果返回给代理服务器,代理服务器再将结果返回给客户端。

正向代理核心:客户端非常明确要访问的服务器地址,客户端是需要配置代理服务的地址、端口、账号密码(如有)等才可使用的。正向代理服务器负责转发客户端的请求到目标服务器。

正向代理主要使用场景是客户端。由于网络不通等物理原因,需要通过正向代理服务器这种转发环节顺利访问目标服务器。当然,也可以通过正向代理对客户端某些信息进行伪装和改变。

在这里插入图片描述

反向代理

反向代理是一种代理服务器的配置模式,它代表服务器向客户端提供服务。客户端发送请求到反向代理服务器,然后反向代理服务器将请求转发到后端的真实服务器上,并将响应返回给客户端。简单理解为用户直接访问反向代理服务器就可以获得目标服务器的资源。这一过程叫反向代理

在这里插入图片描述

nginx的安装使用

Docker版本的nginx安装

下载

在这里插入图片描述

[root@localhost ~]# docker pull nginx
Using default tag: latest
Trying to pull repository docker.io/library/nginx ... 
latest: Pulling from docker.io/library/nginx
a2abf6c4d29d: Already exists 
a9edb18cadd1: Pull complete 
589b7251471a: Pull complete 
186b1aaa4aa6: Pull complete 
b4df32aa5a72: Pull complete 
a0bcbecc962e: Pull complete 
Digest: sha256:0d17b565c37bcbd895e9d92315a05c1c3c9a29f762b011a10c54a66cd53c9b31
Status: Downloaded newer image for docker.io/nginx:latest
[root@localhost ~]# 

创建挂载文件

nginx/conf nginx/logs nginx/html

[root@localhost software]# pwd
/usr/local/software
[root@localhost software]# mkdir -p nginx/logs nginx/cof nginx/html
[root@localhost software]# ls
6380  6381  6389  6390  6391  9361  canal  nacos  nginx  postgres  rabbitmq  redis  sentinel
[root@localhost software]# cd nginx/
[root@localhost nginx]# ls
cof  html  logs
[root@localhost nginx]# 

获取配置文件
创建docker容器
[root@localhost nginx]# docker run -it --name nginx -d nginx
09e7eb76a6e54e14f76a85bc5828af34432d57b33e0e976d5e08c219d7450161

在这里插入图片描述

拷贝容器中的配置文件
docker cp nginx:/etc/nginx/nginx.conf conf/

在这里插入图片描述

[root@localhost nginx]# docker cp nginx:/etc/nginx/nginx.conf conf/
[root@localhost nginx]# cd conf/
[root@localhost conf]# ls
nginx.conf
[root@localhost conf]# docker cp nginx:/etc/nginx/conf.d ./
[root@localhost conf]# ls
conf.d  nginx.conf
[root@localhost conf]# ll
总用量 4
drwxr-xr-x. 2 root root  26 8月  23 21:47 conf.d
-rw-r--r--. 1 root root 648 12月 28 2021 nginx.conf
[root@localhost conf]# 

删除容器
[root@localhost conf]# docker stop nginx 
nginx
[root@localhost conf]# docker rm nginx 
nginx
[root@localhost conf]# 

创建运行容器

两个端口:

一个代理http协议

一个代理TCP

docker run -it \
--name nginx \
-p 9999:9999 \
-p 10999:10999 \
--privileged \
-v /etc/localtime:/etc/localtime \
-v /usr/local/software/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /usr/local/software/nginx/conf/conf.d:/etc/nginx/conf.d \
-v /usr/local/software/nginx/html:/usr/share/nginx/html \
-v /usr/local/software/nginx/logs:/var/log/nginx \
-d nginx

在这里插入图片描述

查看日志

在这里插入图片描述

开放端口

开放两个端口9999和10999

[root@localhost conf]# firewall-cmd --zone=public --add-port=9999/tcp --permanent 
success
[root@localhost conf]# firewall-cmd --zone=public --add-port=10999/tcp --permanent 
success
[root@localhost conf]# firewall-cmd --reload
success
[root@localhost conf]# firewall-cmd --zone=public --list-ports
3306/tcp 15672/tcp 5672/tcp 2375/tcp 8848/tcp 9848/tcp 9999/tcp 10999/tcp
[root@localhost conf]# 


进行代理和测试
 	server {
        listen 9999;
        location /{
            proxy_pass https://www.bilibili.com;
        }
    }

在这里插入图片描述

代理百度测试:http://192.168.111.130:9999/

img
img

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

-1715063279124)]
[外链图片转存中…(img-shEORIaS-1715063279125)]

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 8
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
window 版本 nginx 最新版本 一般结合Tomcat 使用 配置信息如下: #运行用户 user www-data; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; #工作模式及连接数上限 events { use epoll; #epoll是多路复用IO(I/O Multiplexing)中的一种方式,但是仅用于linux2.6以上内核,可以大大提高nginx的性能 worker_connections 1024;#单个后台worker process进程的最大并发链接数 # multi_accept on; } #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { #设定mime类型,类型由mime.type文件定义 include /etc/nginx/mime.types; default_type application/octet-stream; #设定日志格式 access_log /var/log/nginx/access.log; #sendfile 指令指定 nginx 是否调用 sendfile 函数(zero copy 方式)来输出文件,对于普通应用, #必须设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为 off,以平衡磁盘与网络I/O处理速度,降低系统的uptime. sendfile on; #tcp_nopush on; #连接超时时间 #keepalive_timeout 0; keepalive_timeout 65; tcp_nodelay on; #开启gzip压缩 gzip on; gzip_disable "MSIE [1-6]\.(?!.*SV1)"; #设定请求缓冲 client_header_buffer_size 1k; large_client_header_buffers 4 4k; include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; #设定负载均衡的服务器列表 upstream mysvr { #weigth参数表示权值,权值越高被分配到的几率越大 #本机上的Squid开启3128端口 server 192.168.8.1:3128 weight=5; server 192.168.8.2:80 weight=1; server 192.168.8.3:80 weight=6; } server { #侦听80端口 listen 80; #定义使用www.xx.com访问 server_name www.xx.com; #设定本虚拟主机的访问日志 access_log logs/www.xx.com.access.log main; #默认请求 location / { root /root; #定义服务器的默认网站根目录位置 index index.php index.html index.htm; #定义首页索引文件的名称 fastcgi_pass www.xx.com; fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } # 定义错误提示页面 error_page 500 502 503 504 /50x.html; location = /50x.html { root /root; } #静态文件,nginx自己处理 location ~ ^/(images|javascript|js|css|flash|media|static)/ { root /var/www/virtual/htdocs; #过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。 expires 30d; } #PHP 脚本请求全部转发到 FastCGI处理. 使用FastCGI默认配置. location ~ \.php$ { root /root; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /home/www/www$fastcgi_script_name; include fastcgi_params; } #设定查看Nginx状态的地址 location /NginxStatus { stub_status on; access_log on; auth_basic "NginxStatus"; auth_basic_user_file conf/htpasswd; } #禁止访问 .htxxx 文件 location ~ /\.ht { deny all; } } }
要在 Docker Compose 中安装配置 Nginx 来实现 HTTPS 和反向代理,你可以按照以下步骤进行操作: 1. 首先,在你的项目目录下创建一个名为 `docker-compose.yml` 的文件,并添加以下内容: ```yaml version: '3' services: nginx: image: nginx ports: - 80:80 - 443:443 volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./certs:/etc/nginx/certs ``` 这将创建一个名为 `nginx` 的服务,并将容器的 80 和 443 端口映射到主机的相应端口。同时,我们将挂载一个自定义的 `nginx.conf` 配置文件和一个名为 `certs` 的文件夹用于存放 SSL/TLS 证书。 2. 在项目目录下创建一个名为 `nginx.conf` 的配置文件,并添加以下内容作为示例: ```nginx events {} http { server { listen 80; server_name example.com; location / { proxy_pass http://backend:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/certs/cert.pem; ssl_certificate_key /etc/nginx/certs/key.pem; location / { proxy_pass http://backend:8000; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } } ``` 这是一个简单的 Nginx 配置文件示例,其中设置了一个 HTTP 的反向代理和一个 HTTPS 的反向代理。你需要将 `example.com` 替换为你的域名,并将 SSL/TLS 证书文件 `cert.pem` 和 `key.pem` 放入 `certs` 文件夹中。 3. 在你的项目目录下创建一个名为 `certs` 的文件夹,并将正确的 SSL/TLS 证书文件放入其中。 4. 现在,你可以运行 `docker-compose up -d` 命令启动容器。 这样,Nginx 将通过反向代理将所有对于 `example.com` 的请求转发到名为 `backend` 的服务的 8000 端口上。同时,通过 HTTPS 访问时,Nginx 使用了提供的 SSL/TLS 证书来进行加密通信。 请注意,上述步骤只是一个示例,你可能需要根据你的实际需求进行修改和调整。希望这能帮到你!如果有任何进一步的问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值