目录

Nginx轻量级web服务器

前言

一、如何进行nginx的安装

1、首先要检查nginx是否已经安装

2、使用yum命令安装

二、使用nginx进行反向代理

1、首先安装两个tomcat,端口分别是8081和8082

2、配置两个tomcat

3、修改host文件

4、配置nginx服务器

总结


前言

Nginx是一款轻量级Web服务器,也是一款反向代理服务器。使用nginx可以帮助实现前端动静分离,可以作为邮件代理服务器,也可以进行反向代理加速,支持FastCGI,实现简单的负载均衡和容错;同时nginx的模块化的结构,可以更好的过滤不同的请求,对不同的请求进行分发处理;另外nginx还支持SSL 和 TLS SNI。

Nginx支持热部署,启动速度特别快,还可以在不间断服务的情况下对软件版本或配置进行升级,即使运行数月也无需重新启动。在微服务的体系之下,Nginx正在被越来越多的项目采用作为网关来使用,配合Lua做限流、熔断等控制。对于Nginx的初学者可能不太容易理解web服务器究竟能做什么,特别是之前用过Apache服务器的,以为Nginx可以直接处理php、java,实际上并不能。对于大多数使用者来说,Nginx只是一个静态文件服务器或者http请求转发器,它可以把静态文件的请求直接返回静态文件资源,把动态文件的请求转发给后台的处理程序,例如php-fpm、apache、tomcat、jetty等,这些后台服务,即使没有nginx的情况下也是可以直接访问的(有些时候这些服务器是放在防火墙的面,不是直接对外暴露,通过nginx做了转换)。


一、如何进行nginx的安装

仅提供linux系统安装方式(使用yum命令安装)

1、首先要检查nginx是否已经安装

nginx -V

2、使用yum命令安装

由于nginx不在centos的官方软件源下面,所以不可以直接使用yum。要先执行

yum install epel-release

输入“y”,进行安装后,再执行

yum install nginx

一直输入“y”,直到安装成功。

然后查看版本。nginx -v

使用ip访问。http:ip/,如下图显示,说明安装成功。

二、使用nginx进行反向代理

1、首先安装两个tomcat,端口分别是8081和8082

下载tomcat方式略过。建议下载tomcat8.x以及以上版本。

下载后进行解压缩。

tar -zxvf apache-tomcat-8.5.63.tar.gz

2、配置两个tomcat

解压后,把apache-tomcat-8.5.63复制成两份,一份是tomcat8081,一份是tomcat8082,过程如下

cp -r apache-tomcat-8.5.63 tomcat-8081
cp -r apache-tomcat-8.5.63 tomcat-8082

然后对两个tomcat的端口进行修改

<!-- 修改一-->
<Server port="8006" shutdown="SHUTDOWN">
<!-- 修改二-->
<Connector port="8081" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />
<!-- 修改一-->
<Server port="8007" shutdown="SHUTDOWN">
<!-- 修改二-->
<Connector port="8082" protocol="HTTP/1.1"connectionTimeout="20000"redirectPort="8443" />

然后修改两个tomcat里面的webapps文件夹里面的ROOT文件夹里面的index.jsp。修改为下面的内容

tomcat1修改为:

<%--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--%>
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
%>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title><%=request.getServletContext().getServerInfo() %></title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head><body><h1>tomcat8081---index.jsp<h1></body>
</html>

tomcat2修改为:

<%--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements.  See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--%>
<%@ page session="false" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<%
java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy");
request.setAttribute("year", sdf.format(new java.util.Date()));
request.setAttribute("tomcatUrl", "https://tomcat.apache.org/");
request.setAttribute("tomcatDocUrl", "/docs/");
request.setAttribute("tomcatExamplesUrl", "/examples/");
%>
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title><%=request.getServletContext().getServerInfo() %></title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head><body><h1>tomcat8082---index.jsp<h1></body>
</html>

然后分别启动两个tomcat。通过http:ip:8081/、http:ip:8082/访问

到此为止tomcat配置已经搞定

3、修改host文件

4、配置nginx服务器

此时当访问www.nginxtest1.com的时候,就会访问host文件,然后就会去找上面host文件www.sina.com指向ip所对应的的linux服务器,然后www.nginxtest1.com 默认的端口就是80,所以访问www.nginxtest1.com 的时候,就会找到下面的upstream tomcat1,然后下面的upstream tomcat1就会去找server 47.104.84.32:8081,就会找到8081端口的tomcat服务器,然后因为upstream tomcat1的默认访问页是index.jsp,所以就会访问8081端口的tomcat服务器的index.jsp页面(也就是http://47.104.84.32:8081/index.jsp)

此时当访问www.nginxtest2.com的时候,就会访问host文件,然后就会去找上面host文件www.nginxtest2.com指向ip对应的linux服务器,然后www.nginxtest2.com默认的端口就是80,所以访问www.nginxtest2.com 的时候,就会找到下面的upstream tomcat2,然后下面的upstream tomcat2就会去找server 47.104.84.32:8082,就会找到8082端口的tomcat服务器,然后因为upstream tomcat2的默认访问页是index.jsp,所以就会访问8082端口的tomcat服务器的index.jsp页面(也就是http://47.104.84.32:8082/index.jsp)

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;events {worker_connections 1024;
}http {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;tcp_nodelay         on;keepalive_timeout   65;types_hash_max_size 2048;include             /etc/nginx/mime.types;default_type        application/octet-stream;# Load modular configuration files from the /etc/nginx/conf.d directory.# See http://nginx.org/en/docs/ngx_core_module.html#include# for more information.include /etc/nginx/conf.d/*.conf;upstream tomcat1 {server 47.104.84.32:8081;}#配置www.houhu.com:80对应的服务器监听端口upstream tomcat2 {server 47.104.84.32:8082;}server {listen       80;server_name  www.nginxtest1.com;root         /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://tomcat1;#配置默认访问页,这里就会访问到tomcat2里面的那个index.jsp文件里面index index.jsp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}server {listen       80;server_name  www.nginxtest2.com;root         /usr/share/nginx/html;# Load configuration files for the default server block.include /etc/nginx/default.d/*.conf;location / {proxy_pass http://tomcat2;#配置默认访问页,这里就会访问到tomcat2里面的那个index.jsp文件里面index index.jsp;}error_page 404 /404.html;location = /404.html {}error_page 500 502 503 504 /50x.html;location = /50x.html {}}
}

此时访问www.nginxtest1.com到的就是tomcat8081对应的tomcat服务器

访问www.nginxtest2.com到的就是tomcat8082对应的tomcat服务器


总结

以上就是此次给大家分享,有关nginx进行反向代理的配置方法。有什么不懂得欢迎在下方留言。后续会继续更新nginx相关的其他配置,比如说动静分离、负载均衡。所以请继续关注我

轻量级web服务器-Nginx的入门相关推荐

  1. 首个Nginx windows Stable 版--轻量级Web服务器Nginx 0.7.59

    2009.05.25 日晚,Igor Sysoev放出最近被广泛使用的轻量级Web服务器Nginx 0.7系列最新版本0.7.59,同时正式将0.7做为新的Stable稳定系列(0.7.0由19 Ma ...

  2. 轻量级WEB服务器Nginx介绍

    轻量级WEB服务器Nginx介绍 本文目录 第1 章 Nginx 简介 ................................................................ ...

  3. 轻量级HTTP服务器Nginx(配置与调试Nginx)(转)

    2019独角兽企业重金招聘Python工程师标准>>> 转自:http://www.linuxidc.com/Linux/2012-03/55868p3.htm Nginx安装完毕后 ...

  4. 树莓派 Raspberry Pi 3B+ 无线路由器, WEB 服务器(Nginx,PHP,Sqlite3),UART 串口数据采集

    目标: 测试四核 A53 树莓派 Raspberry Pi 3B+ 无线路由器, 轻量级WEB 服务器(Nginx,PHP,Sqlite3),UART 串口数据采集性能 初始配置,部分选项需要sudo ...

  5. web服务器—nginx

    一.nginx介绍 Nginx("engine x")是一款是由俄罗斯的程序设计师Igor Sysoev所开发高性能的 Web和 反向代理服务器,也是一个 IMAP/POP3/SM ...

  6. Linux项目实战C++轻量级Web服务器源码分析TinyWebServer

    目录 文章简介 一. 先跑起来项目 二.再看项目核心 三.逐个击破!立下flag 文章简介 TinyWebServer是Linux下C++轻量级Web服务器,助力初学者快速实践网络编程,搭建属于自己的 ...

  7. 企业Web服务器Nginx应用实战-高俊峰-专题视频课程

    企业Web服务器Nginx应用实战-6850人已学习 课程介绍         nginx,当今流行的web服务器,运维流行web平台,高薪运维,作为一个轻量级的HTTP服务器,Nginx与Apach ...

  8. 树莓派4B搭建轻量级Web服务器 (Nginx,sqlite,php)

    树莓派硬件的配置,包括外置硬盘的挂载和设置,系统的烧录和设置就略了,详情见第一篇文章. 一:更新源安装Nginx服务器 sudo apt-get updatesudo apt-get install ...

  9. web 服务器-Nginx

    文章目录 一.讲在 Nginx 之前 1.1 同步与异步 1.2 阻塞与非阻塞 1.3 epoll 模型(I/O 多路复用) 二.Nginx 详解 2.1 概述 2.2 工作模式 2.2.1 mast ...

最新文章

  1. animation动画不生效_关于CSS3的animation使用的一些坑,需要注意下!
  2. 为什么工作时间长了。技术反而变低了。
  3. ERP员工入登记查询(六)
  4. html标签强制转换位置,王老师html零基础学习笔记第4课——样式初始化+类型转化...
  5. mysql5.0 php_php怎么连接mysql5.0?
  6. 【springboot+easypoi】一行代码搞定excel导入导出
  7. html5教学案例撰写,怎样撰写教育教学案例
  8. 程序员常挂在嘴边的10句话:刚刚还是好的啊!
  9. NMAP网络扫描工具的安装与使用
  10. 数据泵工具导出的步骤(用于Oracle11G和10G之间的相互转换)
  11. 单元格排序_Excel中这8种简单实用的排序方法,很多人都还不会用!
  12. Jensen不等式讲解与证明
  13. 打印出从1到1000的罗马数字
  14. linux 下不错的html编辑器bluefish
  15. 蓝牙怎么调声音大小_苹果蓝牙耳机
  16. 著名设计师的标志(Logo)设计观 1
  17. 《父与子的编程之旅-与小卡特一起学python》第18章
  18. 计算机网络自顶向下-复习
  19. 如何编译android的linux 内核,ubuntu下编译android内核(arm-none-linux-gnueabi-)
  20. Witkey威客平台测评

热门文章

  1. python 两样本T检验
  2. Winform中DataGridView设置前景色、单元格背景色、标题栏样式、禁止改变高宽、不显示空白行、清除选中样式、填充数据源、设置标题、设置单列宽度
  3. 情迁QQ机器人已具备图片发送能力
  4. 用0和1书写新金融体系,DeFi的火焰已无法熄灭
  5. 学物生地对以后学计算机有影响吗,物生地能学临床医学吗?为什么不建议选物生地?...
  6. 力扣404. 左叶子之和
  7. 国外知名的源代码网站(转)
  8. SAP二维码固定大小
  9. 【AIGC】6、Segment Anything | Meta 推出超强悍可分割一切的模型 SAM
  10. RabbitMQ集群搭建、镜像队列、实现高可用负载均衡、Federation Exchange、Federation Queue、Shovel