Nginx笔记 --常用知识

it2023-02-27  98

文章目录

安装YUM安装Docker安装 配置主配置文件Server配置文件Docker映射Nginx配置文件目录 反向代理正向代理概念反向代理概念创建DB和WEB容器验证DB和WEB容器基于Nginx反向代理Tomcatlocation匹配 负载均衡负载均衡策略

安装

YUM安装

参考:http://nginx.org/en/linux_packages.html#RHEL-CentOS

[root@nginx ~]# dnf install yum-utils [root@nginx ~]# cat /etc/yum.repos.d/nginx.repo [nginx-stable] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever/$basearch/ gpgcheck=1 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [root@nginx ~]# dnf install -y nginx [root@nginx ~]#

Docker安装

[root@nginx ~]# mkdir /etc/docker/compose/simple-nginx -p [root@nginx ~]# cd /etc/docker/compose/simple-nginx [root@nginx simple-nginx]# vi docker-compose.yml [root@nginx simple-nginx]# cat docker-compose.yml version: "3.8" services: nginx: restart: always image: nginx:latest container_name: nginx01 ports: - 80:80 [root@nginx simple-nginx]# docker-compose up -d Starting nginx01 ... done [root@nginx simple-nginx]# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 91630da77ad9 nginx:latest "/docker-entrypoint.…" 54 seconds ago Up 15 seconds 0.0.0.0:80->80/tcp nginx01 [root@nginx simple-nginx]#

配置

主配置文件

[root@nginx ~]# docker container exec -it nginx01 bash root@91630da77ad9:/# grep -v "^$" /etc/nginx/nginx.conf user 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; include /etc/nginx/conf.d/*.conf; } root@91630da77ad9:/#

Server配置文件

root@91630da77ad9:/# grep -Ev "^$|#" /etc/nginx/conf.d/default.conf server { listen 80; listen [::]:80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } root@91630da77ad9:/#

Docker映射Nginx配置文件目录

[root@nginx ~]# cd /etc/docker/compose/simple-nginx/ [root@nginx simple-nginx]# vi docker-compose.yml [root@nginx simple-nginx]# tail -2 docker-compose.yml volumes: - /opt/docker_simple_nginx/conf.d/:/etc/nginx/conf.d/ [root@nginx simple-nginx]# docker-compose down Stopping nginx01 ... done Removing nginx01 ... done Removing network simple-nginx_default [root@nginx simple-nginx]# docker-compose build nginx uses an image, skipping [root@nginx simple-nginx]# docker-compose up -d Creating network "simple-nginx_default" with the default driver Creating nginx01 ... done [root@nginx simple-nginx]#

反向代理

正向代理概念

正向代理: 1.正向代理服务是由客户端设立的 2.客户端了解代理服务器和目标服务器都是谁 3.帮助咱们实现突破访问权限,提高访问的速度,对目标服务器隐藏客户端的ip地址

反向代理概念

反向代理: 1.反向代理服务器是配置在服务端的 2.客户端不知道访问的到底是哪一台服务器 3.达到负载均衡,并且可以隐藏服务器真正的ip地址

创建DB和WEB容器

[root@nginx ~]# mkdir /etc/docker/compose/simple_db_web [root@nginx ~]# cd /etc/docker/compose/simple_db_web [root@nginx simple_db_web]# vi docker-compose.yml [root@nginx simple_db_web]# cat docker-compose.yml version: "3.8" services: mysql: restart: always image: mysql:latest container_name: mysql01 ports: - 3306:3306 environment: MYSQL_ROOT_PASSWORD: 123456 TZ: Asia/Shanghai volumes: - /opt/docker_simple_db_web/mysql_data:/var/lib/mysql tomcat: restart: always image: tomcat:latest container_name: tomcat01 ports: - 8080:8080 environment: TZ: Asia/Shanghai volumes: - /opt/docker_simple_db_web/tomcat_webapps:/usr/local/tomcat/webapps - /opt/docker_simple_db_web/tomcat_logs:/usr/local/tomcat/logs [root@nginx simple_db_web]# docker-compose up -d Creating network "simple_db_web_default" with the default driver Creating mysql01 ... done Creating tomcat01 ... done [root@nginx simple_db_web]#

验证DB和WEB容器

[root@nginx ~]# docker container inspect mysql01 | grep IPAddress "SecondaryIPAddresses": null, "IPAddress": "", "IPAddress": "172.20.0.2", [root@nginx ~]# mysql -uroot -h172.20.0.2 -p mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | | sys | +--------------------+ 4 rows in set (0.01 sec) mysql> [root@nginx ~]# cd /opt/docker_simple_db_web/tomcat_webapps/ [root@nginx tomcat_webapps]# mkdir ROOT [root@nginx tomcat_webapps]# echo "Hello World!" > ROOT/index.html [root@nginx tomcat_webapps]# curl localhost:8080 Hello World! [root@nginx tomcat_webapps]#

基于Nginx反向代理Tomcat

[root@nginx ~]# vi /opt/docker_simple_nginx/conf.d/default.conf [root@nginx ~]# cat /opt/docker_simple_nginx/conf.d/default.conf server { listen 80; server_name localhost; location / { proxy_pass http://13.13.100.100:8080/; # 宿主机地址 } } [root@nginx ~]# cd /etc/docker/compose/simple-nginx/ [root@nginx simple-nginx]# docker-compose restart Restarting nginx01 ... done [root@nginx ~]# firewall-cmd --add-port=8080/tcp success [root@nginx ~]# curl localhost Hello World! [root@nginx ~]#

location匹配

参考:https://www.jianshu.com/p/64b2cf647663

负载均衡

Nginx为我们默认提供了三种负载均衡的策略: 1.轮询: 将客户端发起的请求,平均分配给每一台服务器 2.权重: 会将客户端的请求,根据服务器的权重值不同,分配不同的数量 3.ip_hash: 基于发起请求的客户端的ip地址不同,他始终会将请求发送到指定的服务器上 就是说如果这个客户端的请求的ip地址不变,那么处理请求的服务器将一直是同一个

负载均衡策略

轮询 upstream my_server{ server ncthz.top:8080; server ncthz.top:8081; } server { listen 80; listen [::]:80; server_name localhost; location / { proxy_pass http://my_server/; #tomcat首页 } } 权重 upstream my_server{ server ncthz.top:8080 weight=10; server ncthz.top:8081 weight=2; } server { listen 80; listen [::]:80; server_name localhost; location / { proxy_pass http://my_server/; #tomcat首页 } } IP_Hash upstream my_server{ ip_hash; server ncthz.top:8080; server ncthz.top:8081; } server { listen 80; listen [::]:80; server_name localhost; location / { proxy_pass http://my_server/; #tomcat首页 } }
最新回复(0)