轻量级web服务器-Nginx的入门-程序员宅基地

技术标签: Nginx  java  nginx  nginx反向代理  

目录

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/,如下图显示,说明安装成功。

3、Ubuntu系统安装nginx

sudo apt-get install nginx

输入“y”,进行安装后,然后查看版本。nginx -v

 二、使用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 at

    http://www.apache.org/licenses/LICENSE-2.0

Unless 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 at

    http://www.apache.org/licenses/LICENSE-2.0

Unless 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相关的其他配置,比如说动静分离、负载均衡。所以请继续关注我

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/SuperGeekNumber/article/details/113878928

智能推荐

Unity2D 面向目标方向-程序员宅基地

文章浏览阅读1.7k次。在2d空间上,假设角色的自身的y轴方向为正方向,如果要让角色随时面向一个目标点。这里假设(0,0)点为目标点第一种:Vector3 v = Vector3.zero - transform.position; //首先获得目标方向 v.z = 0; ..._unity 2d中哪个方向是forward

基于XDMA 的PCIE读写DDR_xdma ddr-程序员宅基地

文章浏览阅读7.4k次,点赞16次,收藏100次。基于XDMA 的PCIE读写DDR概述:  想实现基于FPGA的PCIe通信,查阅互联网各种转载…基本都是对PCIe的描述,所以想写一下基于XDMA的PCIe通信的实现(PCIe结构仅做简单的描述(笔记),了解详细结构移至互联网)。实现功能:PC通过PCIE读写DDR,同时用户通过逻辑代码可以读取被写入DDR内的数据(我是通过VIO实现DDR任意地址,任意数据大小的读取。)。实践实践!!!说明:参考文档:PCI Express Base Specification Revision 3.0P_xdma ddr

缓存穿透与布隆过滤器_缓存穿透 布隆过滤器-程序员宅基地

文章浏览阅读1.7k次。本文主要介绍在使用缓存过程中经常会遇到的几个问题:缓存击穿、缓存雪崩、缓存穿透,以及其解决方案。之后会对缓存穿透的解决方案之一布隆过滤器,进行详细讲解。_缓存穿透 布隆过滤器

pandas写入oracle,python pandas dataframe 读取和写入Oracle-程序员宅基地

文章浏览阅读1.4k次。1、代码:主要写入时表要为小写,否则报错Could not reflect: requested table(s) not available in Enginefrom sqlalchemy import create_engineconn_string='oracle+cx_oracle://admin:[email protected]:1521/ORCL?charset=utf8'..._pandas cx_oracle 写入

react-dnd拖拽表单组件列表实例_const defaultlist =-程序员宅基地

文章浏览阅读1.1k次,点赞2次,收藏2次。API 学习:https://blog.csdn.net/gaofeng6565/article/details/115696823项目准备:https://codepen.io/选择React,AntDesign 模式,添加依赖 react-dnd(@14.0.2),react-dnd-html5-backend(@14.0.0)ListDrag.jsimport React, { useState } from "react";import { Button } from "antd"._const defaultlist =

无人驾驶之MATLAB无人驾驶工具箱学习(1)_ego vehicle-程序员宅基地

文章浏览阅读2.4w次,点赞21次,收藏162次。更新完显卡驱动后,视频可以自动导入了,继续码。2018.08.111 坐标系转换ADST(Automated Driving System Toolbox)中的坐标系ADST中的坐标系:世界坐标系(world),所有车辆及其传感器都建立其上的固定坐标系。 车辆(Vechicle):固定在车身上。有代表性地,车辆坐标系建立在车辆后轴中点处的地面上。 传感器(Sensor):明确具..._ego vehicle

随便推点

探索网络攻防技术:自学之道_网络攻防自学-程序员宅基地

文章浏览阅读383次。网络攻防技术在不断演进,新的威胁和安全措施不断出现。因此,持续学习和跟进是至关重要的。订阅安全博客、参加安全研讨会、阅读最新的安全报告等方式,可以帮助你保持对新技术和趋势的了解。_网络攻防自学

0-1字典树总结和经典例题(ing)_字典树例题 poj-程序员宅基地

文章浏览阅读1.1k次,点赞2次,收藏5次。Table of Contents0-1字典树例题1. CSU 1216:异或最大值:给定一些数,任意两个数的最大异或值例题2.HDU 4825Xor Sum:每次询问给出一个数,找出一个与它异或结果最大的数例题3.HDU 5536Chip Factory: 计算(s[i] + s[j]) ^ s[k] 的最大值例题4.POJ 3764The xor-longe..._字典树例题 poj

HEVD之栈溢出_通过fs寄存器解决栈溢出-程序员宅基地

文章浏览阅读881次。自学习CVE-2017-11882漏洞后,便开始希望学习更多的漏洞,提高一下对漏洞的理解。在github上找到一个很好的项目,基本涵盖了所有漏洞(https://github.com/hacksysteam/HackSysExtremeVulnerableDriver)。下载下来后,执行过后得到驱动文件HEVD.sys,以及漏洞利用程序HackSysEVDExploit.exe,然后开启双..._通过fs寄存器解决栈溢出

【漏洞复现-通达OA】通达OA share身份认证绕过漏洞_tongda oa 身份绕过-程序员宅基地

文章浏览阅读364次。通达OA(Office Anywhere网络智能办公系统)是中国通达公司的一套协同办公自动化软件。通达OA /share/handle.php存在一个认证绕过漏洞,利用该漏洞可以实现任意用户登录。攻击者可以通过构造恶意攻击代码,成功登录系统管理员账户,继而在系统后台上传恶意文件控制网站服务器。_tongda oa 身份绕过

Java使用LocalDate获取某个月的第一天和最后一天日期_localdate获取当月最后一天-程序员宅基地

文章浏览阅读3.8w次,点赞33次,收藏80次。Java使用LocalDate或LocalDateTime获取某个月的第一天和最后一天日期_localdate获取当月最后一天

银河麒麟ARM64 飞腾FT2000 linuxdeployqt linux打包qt_linuxdeployqt aarch64-程序员宅基地

文章浏览阅读6.2k次,点赞3次,收藏40次。银河麒麟ARM64 飞腾FT2000 linuxdeployqt linux打包qt下载linuxdeployqt-aarch64.AppImageqt版本说明linuxdeployqt打包准备编译好的程序插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入下载linuxdeployqt-aa_linuxdeployqt aarch64

推荐文章

热门文章

相关标签