centos7 搭建Nginx-1.12.2代理服务器

爱被打了一巴掌 2022-06-02 01:28 254阅读 0赞

一、准备工作(选择分步安装与集中安装,其中一种即可**)**

a.分步安装:

1.安装 gcc

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装

# yum install gcc-c++

2.安装 zlib

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

# yum install -y pcre pcre-devel

3.安装 openssl

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

# yum install -y openssl openssl-devel

4.安装 pcre

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库

# yum install -y pcre pcre-devel

5.安装autoconf automake

# yum install -y autoconf automake

b.集成安装
# yum -y install gcc gcc-c++ autoconf automake
# yum -y install zlib zlib-devel openssl openssl-devel pcre-devel

二、安装启动Nginx

1.下载

# wget http://nginx.org/download/nginx-1.12.2.tar.gz

2.解压

# tar -zxvf nginx-1.12.2.tar.gz

3.编译

# cd nginx-1.12.2/

# ./configure

4.安装

# make

# make install

5.启动

# cd /usr/local/nginx/sbin

# ./nginx

6.查看端口

# netstat -ntlp

  1. Active Internet connections (only servers)
  2. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  3. tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 6765/nginx: master
  4. tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 2455/dnsmasq
  5. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1168/sshd
  6. tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1165/cupsd

三、测试

1.访问 http://localhost 出现下图所示信息,即表明nginx 安装启动成功

Center

四、安装 tomcat 为测试Nginx反向代理

1.下载

# wget http://mirrors.shuosc.org/apache/tomcat/tomcat-8/v8.0.48/bin/apache-tomcat-8.0.48.tar.gz

2.解压

# tar -zxvf apache-tomcat-8.0.48.tar.gz

3.启动测试

# cd /apache-tomcat-8.0.48/bin

# ./startup.sh

  1. [root@localhost bin]# ./startup.sh
  2. Using CATALINA_BASE: /usr/software/apache-tomcat-8.0.48
  3. Using CATALINA_HOME: /usr/software/apache-tomcat-8.0.48
  4. Using CATALINA_TMPDIR: /usr/software/apache-tomcat-8.0.48/temp
  5. Using JRE_HOME: /usr/java/jdk1.8.0_121/jre
  6. Using CLASSPATH: /usr/software/apache-tomcat-8.0.48/bin/bootstrap.jar:/usr/software/apache-tomcat-8.0.48/bin/tomcat-juli.jar
  7. Tomcat started.

启动成功 访问:http://localhost:8080

Center 1

4.打开 nginx的配置文件

# cd /usr/local/nginx/conf

复制一份配置文件 命名为: nginx.conf.bak

#cp nginx.conf nginx.conf.bak

# vim nginx.conf

找到: location / 处,添加: proxy_pass http://localhost:8080; http://localhost:8080;为tomcat的访问路径

  1. server {
  2. listen 80; #nginx的监听端口
  3. server_name localhost;
  4. #charset koi8-r;
  5. #access_log logs/host.access.log main;
  6. location / {
  7. root html;
  8. index index.html index.htm;
  9. proxy_pass http://localhost:8080;
  10. }
  11. #error_page 404 /404.html;
  12. # redirect server error pages to the static page /50x.html
  13. #
  14. error_page 500 502 503 504 /50x.html;
  15. location = /50x.html {
  16. root html;
  17. }

修改之后,保存。重启nginx

# ./usr/local/nginx/sbin/nginx -s reload

访问:http://localhost:80 即可访问 tomcat的路径

Center 2

四、Nginx 负载均衡

1.此处需要部署两个tomcat 进入到第四步载的tomcat的路径下

# cp -r apache-tomcat-8.0.48 tomcat1

# cp -r apache-tomcat-8.0.48 tomcat2

2.修改tomcat配置文件

tomcat1 端口设定为8080 ;tomcat1的配置无需修改

tomcat2 端口设定为8090

# cd /tomcat2/conf

# vim server.xml

修改

为:

为:

为:

3.在tomcat1和tomcat2下创建测试站点

# cd /tomcat1/webapp

# madir test

# vi index.html

添加

this is tomcat1 index

esc :wq 保存推出

同理在tomcat2下创建test与indexhtml,添加this is tomcat2 index

# cd /tomcat2/webapp

# madir test

# vi index.html

添加

this is tomcat2 index

esc :wq 保存推出

4.修改Nginx的 nginx.conf配置文件

# vim /usr/local/nginx/conf/nginx.conf

  1. http {
  2. include mime.types;
  3. default_type application/octet-stream;
  4. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  5. # '$status $body_bytes_sent "$http_referer" '
  6. # '"$http_user_agent" "$http_x_forwarded_for"';
  7. #access_log logs/access.log main;
  8. sendfile on;
  9. #tcp_nopush on;
  10. #keepalive_timeout 0;
  11. keepalive_timeout 65;
  12. #gzip on;
  13. upstream mytomcat { server localhost:8080 weight=100; server localhost:8090 weight=110; } server {
  14. listen 80;
  15. server_name mytomcat;
  16. #charset koi8-r;
  17. #access_log logs/host.access.log main;
  18. location / {
  19. root html;
  20. index index.html index.htm;
  21. proxy_pass http://mytomcat;
  22. }
  23. #error_page 404 /404.html;
  24. # redirect server error pages to the static page /50x.html
  25. #
  26. error_page 500 502 503 504 /50x.html;
  27. 保存后退出
  28. 分别启动tomcat1tomcat2
  29. # /tomcat1/bin/startup.sh
  30. # /tomcat2/bin/startup.sh
  31. 分别访问 http://localhost:8080/test1与http://localhost:8090/test2 验证是否正常
  32. 重启 nginx
  33. # /usr/local/nginx/sbin/nginx -s reload
  34. 打开浏览器输入:http://localhost/test 多刷新几次,就会看到请求回来的内容有时不一样。说明,nginx 是随机访问的tomcat1或者tomcat2
  35. 配置文件里面的weight哪个数值越大,请求到的次数就会多。

发表评论

表情:
评论列表 (有 0 条评论,254人围观)

还没有评论,来说两句吧...

相关阅读