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