腾讯云(五)nginx

A、反向代理

一台服务器需要部署多个web应用且每个应用都想使用80端口直接访问,可以使用nginx做反向代理。

一.安装nginx

1.安装依赖
#gcc安装,nginx源码编译需要
yum install gcc-c++

#PCRE pcre-devel 安装,nginx 的 http 模块使用 pcre 来解析正则表达式
yum install -y pcre pcre-devel

#zlib安装,nginx 使用zlib对http包的内容进行gzip
yum install -y zlib zlib-devel

#OpenSSL 安装,强大的安全套接字层密码库,nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http)
yum install -y openssl openssl-devel
2.下载
#下载版本号可根据目前官网最新稳定版自行调整
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
3.安装,启动
#根目录使用ls命令可以看到下载的nginx压缩包,然后解压
tar -zxvf nginx-1.16.1.tar.gz

#解压后进入目录
cd nginx-1.16.1

#使用默认配置
./configure

#编译安装
make
make install

#查找安装路径,默认都是这个路径
[root@VM_0_12_centos ~]# whereis nginx
nginx: /usr/local/nginx

#启动、停止nginx
cd /usr/local/nginx/sbin/
./nginx     #启动
./nginx -s stop  #停止,直接查找nginx进程id再使用kill命令强制杀掉进程
./nginx -s quit  #退出停止,等待nginx进程处理完任务再进行停止
./nginx -s reload  #重新加载配置文件,修改nginx.conf后使用该命令,新配置即可生效

#重启nginx,建议先停止,再启动
./nginx -s stop
./nginx

#查看nginx进程,如下返回,即为成功
[root@VM_0_12_centos ~]# ps aux|grep nginx
root      5984  0.0  0.0 112708   976 pts/1    R+   14:41   0:00 grep --color=auto nginx
root     18198  0.0  0.0  20552   612 ?        Ss   11:28   0:00 nginx: master process ./nginx
nobody   18199  0.0  0.0  23088  1632 ?        S    11:28   0:00 nginx: worker process
4.直接访问服务器的域名/IP即可
5.配置开机自启动
#在rc.local增加启动代码即可
vim /etc/rc.local
#增加一行 /usr/local/nginx/sbin/nginx,增加后保存
#设置执行权限
cd /etc
chmod 755 rc.local

二、配置反向代理

1.修改配置文件

例如:tomcat下有两个web应用:
web1:域名:www.jiweiwei2020.top,使用8080端口;
web2:域名:www.moonknightsoft.com,使用 8090端口;

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  www.jiweiwei2020.top;

        location / {
            root   html;
            index  index.html index.htm;
			proxy_pass http://127.0.0.1:8080;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
	
    server {
        listen       80;
       # 1.修改域名
        server_name  www.moonknightsoft.com;

        location / {
           root   html;
           index  index.html index.htm;
        # 2.修改端口号
		   proxy_pass http://127.0.0.1:8090;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

2.重启nginx
 /usr/local/nginx/sbin/nginx -s reload

B、负载均衡

http {
    upstream upstream_name{
     	# 配置多台应用服务器
        server 192.168.0.28:8001;
        server 192.168.0.28:8002;
    }

    server {
    	# 配置nginx监听的端口(将此端口的请求转发到应用服务器)
        listen       8080;
        server_name  localhost;

        location / {
        	# 这里的upstream_name要与上面应用服务器配置的upstream_name一样
            proxy_pass http://upstream_name;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }
}

我这里没有把默认的一些配置贴出来。
首先,在http下添加 upstream upstream_name {} 来配置要映射的服务器。
其中的upstream_name大家可以指定为服务的域名或者项目的代号。
server下的location 我们将 / 下的全部请求转发到 http://upstream_name ,也就是我们上面配置的服务器列表中的某一台服务器上。具体是哪台服务器,nginx会根据配置的调度算法来确认。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值