1修改hosts文件

127.0.0.1    www.test1.com
127.0.0.1 www.test2.com
127.0.0.1 static.com

2配置tomcat的server.xml

<?xml version='1.0' encoding='utf-8'?>

<Server port="8005" shutdown="SHUTDOWN">
<Listener className="org.apache.catalina.startup.VersionLoggerListener" /> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
<Listener className="org.apache.catalina.core.JasperListener" />
<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
<Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />
<Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <GlobalNamingResources> <Resource name="UserDatabase" auth="Container"
type="org.apache.catalina.UserDatabase"
description="User database that can be updated and saved"
factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
pathname="conf/tomcat-users.xml" />
</GlobalNamingResources> <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" /> <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /> <Engine name="Catalina" defaultHost="localhost"> <Realm className="org.apache.catalina.realm.LockOutRealm">
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm> <Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
prefix="localhost_access_log." suffix=".txt"
pattern="%h %l %u %t &quot;%r&quot; %s %b" /> </Host> <Host name="www.test1.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"> <Context path ="" docBase ="/test1" debug ="0" reloadbale ="true" ></Context> </Host> <Host name="www.test2.com" appBase="webapps"
unpackWARs="true" autoDeploy="true"> <Context path ="" docBase ="/test2" debug ="0" reloadbale ="true" ></Context> </Host>
</Engine>
</Service>
</Server>

将test1和test2项目放在tomcat根目录/webapps下(项目用war包或者war解压后的目录文件都行)

3配置nginx的nginx.conf文件

 #user  nobody;
worker_processes 1; #error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid logs/nginx.pid; events {
worker_connections 1024;
} http {
include mime.types;
default_type application/octet-stream; #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 logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} upstream Tomcat_client {
server localhost:8080;
}
server {
listen 80;
server_name www.test1.com; location / {
proxy_pass http://Tomcat_client;
proxy_redirect default;
#设置代理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
} server {
listen 80;
server_name www.test2.com;
location / {
proxy_pass http://Tomcat_client;
proxy_redirect default;
#设置代理
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
} server {
listen 80;
server_name static.com; location / {
root C:/image;
autoindex on; }
} }

主要关注116~153行;启动toncat和nginx,在C盘下建image文件,可通过http://static.com/直接访问静态资源。也可在其他html页面访问静态资源

浏览器输入www.test1.com回车,因为http默认端口为80,所以相当于输入的是www.test1.com:80 而nginx监听了80端口,找到server_name为www.test1.com的server,然后将请求交给被代理的tomcat服务器,如136行所示,Tomcat_client实际上表示localhost:8080

nginx简单实现反向代理和静态资源服务器的更多相关文章

  1. 二.Nginx反向代理和静态资源服务配置

    2018年03月31日 10:30:12 麦洛_ 阅读数:1362更多 所属专栏: nginx   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/M ...

  2. Nginx反向代理Tomcat静态资源无法加载以及请求链接错误

     在使用Nginx实现Tomcat的负载均衡的时候,项目发布到了Tomcat,Nginx也配置好了, 当访问的时候发现了与预期不符 表现为: 静态资源加载失败 链接跳转地址错误 下面是我错误的配置文件 ...

  3. nginx 配置反向代理和静态资源

    https://unit.nginx.org/integration/ 与NGINX集成 在NGINX后面安装单元 将NGINX配置为静态Web服务器,并在Unit前面配置反向代理. NGINX直接从 ...

  4. Nginx 简单配置反向代理

    nginx 配置反向代理,转发请求到另一个域名 在server中加入 location /${alias} { proxy_buffer_size 128k; proxy_buffers 32 32k ...

  5. 循序渐进nginx(二):反向代理、负载均衡、缓存服务、静态资源访问

    目录 反向代理 使用 1.创建代理目标服务端: 2.配置nginx反向代理目标服务端: 3.测试使用: 负载均衡 使用 1.准备服务端 2.修改nginx配置 3.测试 负载均衡策略 负载均衡的额外参 ...

  6. 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器

    第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...

  7. Nginx之最简单的反向代理机制分析

    注:当前分析基于 Nginx之搭建反向代理实现tomcat分布式集群 的配置. 1. 用到的指令 下面介绍在上面的配置中用到的指令. upstream 指令 语法:upstream name { .. ...

  8. Nginx+Apache实现反向代理

    一 反向代理 1.1 反向代理是什么 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器, 并将从服务器上得到的结果返回给 ...

  9. Nginx安装配置&反向代理

    使用Nginx作为前端服务能够更快更及时的响应静态页面.js.图片等,当客户端请求访问动态页面时由Nginx的反向代理给Apache处理,Apache处理完再交予Nginx返回给客户端. Nginx更 ...

随机推荐

  1. CF911F Tree Destruction 解题报告

    CF911F Tree Destruction 题意翻译 给你一棵树,每次挑选这棵树的两个叶子,加上他们之间的边数(距离),然后将其中一个点去掉,问你边数(距离)之和最大可以是多少. 输入输出格式 输 ...

  2. Tomcat学习笔记(五)

    生命周期事件 Catalina包含有很多组件.当Catalina启动时,这些组件也会启动,同样,当Catalina关闭时,这些组件也随之关闭,通过实现org.apache.catalina.Lifec ...

  3. Java正则表达式--Matcher.group函数的用法

    原来,group是针对()来说的,group(0)就是指的整个串,group(1) 指的是第一个括号里的东西,group(2)指的第二个括号里的东西. 最近学习正则表达式,发现Java中的一些术语与其 ...

  4. Android的CheckBox(多选框)

    1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...

  5. ios 改变图片大小缩放方法

    http://www.cnblogs.com/zhangdadi/archive/2012/11/17/2774919.html http://bbs.csdn.net/topics/39089858 ...

  6. 牛客网 暑期ACM多校训练营(第二场)J.farm-STL(vector)+二维树状数组区间更新、单点查询 or 大暴力?

    开心.jpg J.farm 先解释一下题意,题意就是一个n*m的矩形区域,每个点代表一个植物,然后不同的植物对应不同的适合的肥料k,如果植物被撒上不适合的肥料就会死掉.然后题目将每个点适合的肥料种类( ...

  7. (7)oracle数据类型

    字符型 char 定长 最大2000字符   例如 char(20) 表示定长20  不够的补空格   定长查询速度快 varchar2 变长 最大4000字符  省空间 clob 字符型大对象 最大 ...

  8. Python与数据库[1] -> 数据库接口/DB-API[0] -> 通用标准

    数据库接口 / DB-API 在Python中,数据库是通过适配器(Adaptor)来连接访问数据库的,适配器通常与数据库客户端接口(通常为C语言编写)想连接,而不同的适配器都会尽量满足相同的DB-A ...

  9. 代码编辑器[0] -> Vim/gVim[1] -> Vim 的快捷键操作

    快捷键 / Shortcut Keys 1 基本操作 / Basic Operation Vim的基本操作主要可以参考以下几张图,参考链接, 命令行模式 i             从光标所在字符前插 ...

  10. Tarjan缩点【p1726】上白泽慧音

    Description 在幻想乡,上白泽慧音是以知识渊博闻名的老师.春雪异变导致人间之里的很多道路都被大雪堵塞,使有的学生不能顺利地到达慧音所在的村庄.因此慧音决定换一个能够聚集最多人数的村庄作为新的 ...