前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >nginx代理网卡_Nginx学完了!!! —Java133天学习

nginx代理网卡_Nginx学完了!!! —Java133天学习

作者头像
Java架构师必看
发布2021-08-23 10:44:45
9060
发布2021-08-23 10:44:45
举报
文章被收录于专栏:Java架构师必看Java架构师必看

第160次(Nginx)

学习主题:Nginx

学习目标:

1 掌握Nginx在Linux下的各种配置

2 掌握使用Nginx实现负载均衡,反向代理

对应作业

  1. Linux绑定多IP
    1. 如何在Linux中绑定多IP?(写出步骤)
  2. 一台 Linux 服务器绑定两个 ip:192.168.70.144、192.168.70.188 访问不同的 ip 请求不同的 html 目录,

即:

访问 http://192.168.70.144 将访问“html144”目录下的 html 网页

访问 http://192.168.70.188 将访问“html188”目录下的 html 网页

  1. Linux 操作系统允许绑定多 IP。使用 IP 别名的方式,在一块物理网卡上可以绑定多个 lP 地址。这样就能够在使用单一网卡的同一个服务器上运行多个基于 IP 的虚拟主机。但是 在绑定多 IP 时需要将动态的 IP 分配方式修改为静态的指定 IP
  2. cd /etc/sysconfig/network-scripts

IPADDR=192.168.10.144

NETMASK=255.255.255.0

GATEWAY=192.168.10.2

DNS1=114.114.114.114

  1. 将/etc/sysconfig/network-scripts/ifcfg-eth0 文件复制一份,命名为 ifcfg-eth0:1

修改其中内容: DEVICE=eth0:1

IPADDR=192.168.70.188

其他项不用修改 重启系统

  1. Nginx基于IP的虚拟主机配置
    1. 如何配置Nginx的基于IP访问的虚拟主机?(写出步骤)
ac320661915a69c144f9b08b03b8b430.png
ac320661915a69c144f9b08b03b8b430.png

user root;

worker_processes 1;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

#log_format main 'remote_addr - remote_user [time_local] "

# 'status body_bytes_sent "

# '"

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name 192.168.193.129;

location / {

root html129;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name 192.168.193.130;

location / {

root html130;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

  1. Nginx基于端口的虚拟主机配置
    1. 如何配置Nginx的基于端口访问的虚拟主机?(写出步骤)

user root;

worker_processes 1;

#error_log logs/error.log;

#error_log logs/error.log notice;

#error_log logs/error.log info;

#pid logs/nginx.pid;

events {

worker_connections 1024;

}

http {

include mime.types;

default_type application/octet-stream;

#log_format main 'remote_addr - remote_user [time_local] "

# 'status body_bytes_sent "

# '"

#access_log logs/access.log main;

sendfile on;

#tcp_nopush on;

#keepalive_timeout 0;

keepalive_timeout 65;

#gzip on;

server {

listen 80;

server_name 192.168.193.129;

location / {

root html129;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 80;

server_name 192.168.193.130;

location / {

root html130;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#这里是配置端口的

server {

listen 4444;

server_name 192.168.193.129;

location / {

root html4444;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

server {

listen 5555;

server_name 192.168.193.129;

location / {

root html5555;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

}

重启Nginx:

./nginx -s quit 关闭

./nginx 启动

  1. Nginx基于域名的虚拟主机配置
    1. 如何配置Nginx的基于域名访问的虚拟主机?(写出步骤)

文件路径:C:WindowsSystem32driversetc (获取管理员权限否则不能修改)

6ff36c8444eda74748783525fc9a87e3.png
6ff36c8444eda74748783525fc9a87e3.png

配置Nginx(这里主要是server的配置)

#配置百度

server {

listen 80;

server_name www.baidu.com;

location / {

root htmlbaidu;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#配置淘宝

server {

listen 80;

server_name www.taobao.com;

location / {

root htmltaobao;

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

  1. Nginx反向代理-配置反向代理
    1. 如何通过Nginx配置服务的反向代理?(写出步骤)_

2.1需求

安装两个 tomcat 服务,通过 nginx 反向代理。 本案例中使用两台虚拟机演示。 tomcat 安装到 192.168.70.143 环境中。端口为 8080 与 9090 Nginx 安装在 192.168.70.144 环境中

2.2安装环境

2.3安装 tomcat

9079f674de9a0c40aee4a8dd452d3d3c.png
9079f674de9a0c40aee4a8dd452d3d3c.png

2.4配置 tomcat

2.4.1修改端口

c497ef48c72059c7b49fe9b1faf23caf.png
c497ef48c72059c7b49fe9b1faf23caf.png

2.4.2修改首页内容

d637a8ff0c49cbf2eeef196bf766eaf2.png
d637a8ff0c49cbf2eeef196bf766eaf2.png

2.5配置 Nginx 实现服务的反向代理

nginx.conf

user root; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events {

worker_connections 1024; } http {

include mime.types;

default_type application/octet-stream;

sendfile on;

keepalive_timeout 65;

upstream tomcat_server1{ server 192.168.70.143:8080;

}

upstream tomcat_server2{ server 192.168.70.143:9090;

}

server {

listen 80; #为虚拟机指定 IP 或者是域名

server_name test.bjsxt.com; #主要配置路由访问信息

location / {

#用于指定访问根目录时,访问虚拟主机的 web 目录

proxy_pass http://tomcat_server1;

#在不指定访问具体资源时,默认的展示资源的列表 index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

}

#一个 Server 就是一个虚拟主机

server {

listen 80; #为虚拟机指定 IP 或者是域名

server_name test.itbaizhan.cn; #主要配置路由访问信息

location / {

#用于指定访问根目录时,访问虚拟主机的 web 目录

proxy_pass http://tomcat_server2;

#在不指定访问具体资源时,默认的展示资源的列表

index index.html index.htm;

}

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root html;

}

} }

  1. Nginx反向代理-配置负载均衡
    1. 什么是负载均衡?

负载均衡建立在现有网络结构之上,它提供了一种廉价有效透明的方法扩展 网络设备和服务器的带宽、增加吞吐量、加强网络数据处理能力、提高网络的灵活性和可用性。

负载均衡,英文名称为 Load Balance,其意思就是分摊到多个操作单元上 进行执行,例如 Web 服务器、FTP 服务器、企业关键应用服务器和其它关键任务 服务器等,从而共同完成工作任务。

  1. Nginx中所支持的负载均衡策略有哪些?

轮询(默认)

每个请求按时间顺序逐一分配到不同的后端服务器,如果后 端服务器 down 掉,能自动剔除。

指定权重

指定轮询几率,weight 和访问比率成正比,用于后端服务器 性能不均的情况。 upstream backserver { server 192.168.0.14 weight=10; server 192.168.0.15 weight=10; }

  1. Nginx中默认的负载均衡策略是什么?

轮询(默认)

  1. 如何在Nginx中指定权重?

指定轮询几率,weight 和访问比率成正比,用于后端服务器 性能不均的情况。 upstream backserver { server 192.168.0.14 weight=10; server 192.168.0.15 weight=10; }

  1. Nginx反向代理-HTTP协议代理
    1. 如何通过Nginx解决上传图片后无法回显的问题?

明天再改.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 学习主题:Nginx
  • 学习目标:
相关产品与服务
轻量应用服务器
轻量应用服务器(TencentCloud Lighthouse)是新一代开箱即用、面向轻量应用场景的云服务器产品,助力中小企业和开发者便捷高效的在云端构建网站、Web应用、小程序/小游戏、游戏服、电商应用、云盘/图床和开发测试环境,相比普通云服务器更加简单易用且更贴近应用,以套餐形式整体售卖云资源并提供高带宽流量包,将热门软件打包实现一键构建应用,提供极简上云体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档