首页 技术 正文
技术 2022年11月15日
0 收藏 608 点赞 3,792 浏览 3883 个字

本片博客记录在ubuntu16下安装nginx,以及如何实现负载均衡

安装nginx

  • 如果是新机器,安装相关依赖环境
sudo apt install build-essential
sudo apt install libtool
sudo apt install libpcre3 libpcre3-dev
sudo apt install zlib1g-dev
sudo apt-get install openssl libssl-dev

如果没有依赖环境,编译的时候会报下面的错

make: *** No rule to make target 'build', needed by 'default'.  Stop.

2 下载nginx 到虚拟机

当然我是直接通过filezilla扔给虚拟机的

3 解压,顺道删除压缩包

tar xvf XXXrm -rf XXX

4 进入nginx目录里面,指定安装的目录

指定安装在 /opt/nginx目录下(一般都把自己的软件安装在opt下)

./configure --prefix=/opt/nginx --sbin-path=/usr/bin/nginx

5 编译安装

make && make install

6 启动

 nginx

ok ,现在可以去访问虚拟机的80端口,成功看到nginx的 欢迎页

7 停止

nginx -s stop

nginx号称7×24小时不停止,所以当我们修改配置文件之后,重新加载nginx,执行下面的脚本就好了

nginx -s reload

反向代理

1. 什么是正向代理?:

先说一下啥是正向代理,其实我们用的vpn本质上就是正向代理,因为一堵墙围着我们国家,所以我们通过浏览器访问国外的谷歌时请求被强了,根本出不去,那么我们怎么请求呢?vpn出现了,他就是一种正向代理,我们把我们的请求发送给vpn所在的服务器,比如它在香港,让他帮我们转发给谷歌,谷歌接收到请求后把数据回显给vpn,vpn发送给我们,这其实就是一个正向代理的过程

  • 正向代理特点: 代理服务器起到一个转发请求的作用,但是从一开始它就很明确的知道自己要去访问哪台服务器

2. 什么是反向代理?

  • 同样它的工作也是负责转发;来自客户端的请求,但是当客户端的请求发过来之后,一开始它是不清楚往哪里转发的,他需要根据配置去解析,然后再定位,转发

3. 为什么要使用反向代理?

现在流行的微服务架构,拥有相同功能的服务,通常需要做成集群,那么问题来了,我们的浏览器默认是80端口,多个功能相同的应用分别占用多个不同的端口,那谁来占用80呢?nginx通过反向代理优秀的解决了这个问题

{% asset_img 1.jpg my first image %}

4. nginx如何实现反向代理

通过配置实现

nginx的主配置文件名叫

nginx.config

它在nginx的安装目录下的config目录下面


#user nobody;
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块主要有三个作用域, http server location
http {#可以嵌套多个server ,配置代理,缓存,日志,等绝大多数功能,和第三方模块配置
include 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 logs/access.log main;# 开启高效文件传输模式,这个指令,指定了nginx是否调用sendfile函数传输文件,对于普通的应用设为on ,如果用来进行下载等IO重负载应用可设为off
sendfile on;
#tcp_nopush on; #防止网络阻塞 #keepalive_timeout 0;
keepalive_timeout 65; # 长连接超时时常,单位秒 #gzip on;# 负载均衡
# upstream XXX.com{
# upstream负载均衡块, weight表示权重, 值越大,被分配到的几率就越大
# server 192.168.80.121:80 weight=3;
# server 192.168.80.121:80 weight=3;
# server 192.168.80.121:80 weight=3;
#}#server配置虚拟主机的相关参数
server {
listen 80; # 监听端口 server_name localhost; # 监听地址,可以采用域名加多个空格隔开,如果匹配不到我们在浏览器输入的域名.默认走 localhost, 然后location到nginx的欢迎页 #charset koi8-r; # 本虚拟机的访问日志
#access_log logs/host.access.log main;# location 配置请求的路由,和各种页面的处理情况 location / {
root html;
index index.html index.htm; # 默认页面
} #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 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;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#}}

我们要实现反向代理就是要

  • 新增我们自己的server
server {
listen 80;
server_name 浏览器可能访问的域名1; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / {
proxy_pass http://127.0.0.1:9001; # 转发路径
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}
server {
listen 80;
server_name 浏览器可能访问的域名2; proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; location / {
proxy_pass http://127.0.0.1:10011; # 转发路径
proxy_connect_timeout 600;
proxy_read_timeout 600;
}
}

nginx 通过监听80端口 进而监听我们修改后的域名,成功匹配后呢,通过location转发到我们自定义的路径

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,023
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,513
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,361
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,143
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,774
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,853