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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MyWebServer v3.1.29更新日志: 增加实时流量查看,完善NT服务功能,增加静默启动(加/s命令行启动时不显示主窗口),优化一些网络参数 MyWebServer是一个高性能、易用、小巧、绿色的轻量级WEB服务器软件,是你快速建站及个人HTTP文件服务器的难得工具。支持HTTP/1.1、断点续传、大文件下载、正则表达式URL重写、虚拟目录、HTTP反向代理等,可通过ISAPI接口、FastCGI接口实现执行服务器脚本(如PHP,asp,asp.net等),性能完全超越IIS等很多主流WEB服务器软件。   MyWebServer使用说明: 使用FastCGI时,在映射设置中将映射模块设置为启动FastCGI的命令,且命令行中必须包含IP:port格式(如:127.0.0.1:8988)的服务器信息,当不需要WEB服务器启动FastCGI时,命令行中填入IP:port格式的FastCGI服务器信息即可。 如果使用ISAPI接口,指定ISAPI的DLL文件即可。   注(本服务器不集成任何动态脚本支持,要使用请自行安装):asp支持可安装IASP(该软件要求安装java运行环境)通过isapi接口实现;PHP通过isapi和FastCGI接口均可(isapi方式建议使用PHP 5.2,因为5.3以上版不再提供ISAPI支持);asp.net支持可安装mono然后通过FasctCGI接口实现。上述脚本已测试过可以运行。   URL重写命令(使用基于VBScript的正则表达式): ifsve  如果匹配指定的服务器变量则往下执行,否则执行下条exitr之后的规则。(目前仅支持HTTP_HOST REMOTE_ADDR HTTP_REFERER三个服务器变量) ifurl  如果匹配请求的URL则执行wrurl重写命令,否则执行下条exitr之后的规则。 wrurl  执行URL重写 exitr  结束url重写,不再往下执行。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

双木林L

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值