nginx简单实现反向代理和静态资源服务器
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 "%r" %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简单实现反向代理和静态资源服务器的更多相关文章
- 二.Nginx反向代理和静态资源服务配置
2018年03月31日 10:30:12 麦洛_ 阅读数:1362更多 所属专栏: nginx 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/M ...
- Nginx反向代理Tomcat静态资源无法加载以及请求链接错误
在使用Nginx实现Tomcat的负载均衡的时候,项目发布到了Tomcat,Nginx也配置好了, 当访问的时候发现了与预期不符 表现为: 静态资源加载失败 链接跳转地址错误 下面是我错误的配置文件 ...
- nginx 配置反向代理和静态资源
https://unit.nginx.org/integration/ 与NGINX集成 在NGINX后面安装单元 将NGINX配置为静态Web服务器,并在Unit前面配置反向代理. NGINX直接从 ...
- Nginx 简单配置反向代理
nginx 配置反向代理,转发请求到另一个域名 在server中加入 location /${alias} { proxy_buffer_size 128k; proxy_buffers 32 32k ...
- 循序渐进nginx(二):反向代理、负载均衡、缓存服务、静态资源访问
目录 反向代理 使用 1.创建代理目标服务端: 2.配置nginx反向代理目标服务端: 3.测试使用: 负载均衡 使用 1.准备服务端 2.修改nginx配置 3.测试 负载均衡策略 负载均衡的额外参 ...
- 初始nginx(启动运行) 使用nginx做一个简单的静态资源服务器
第一次接触nginx的时候,那时候公司还是用的一些不知名的小技术,后来公司发展问题,重新招了人,然后接触到nginx,公司 使用nginx用来做代理服务器,所有请求 都先经过nginx服务器,然后交由 ...
- Nginx之最简单的反向代理机制分析
注:当前分析基于 Nginx之搭建反向代理实现tomcat分布式集群 的配置. 1. 用到的指令 下面介绍在上面的配置中用到的指令. upstream 指令 语法:upstream name { .. ...
- Nginx+Apache实现反向代理
一 反向代理 1.1 反向代理是什么 反向代理(Reverse Proxy)方式是指以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器, 并将从服务器上得到的结果返回给 ...
- Nginx安装配置&反向代理
使用Nginx作为前端服务能够更快更及时的响应静态页面.js.图片等,当客户端请求访问动态页面时由Nginx的反向代理给Apache处理,Apache处理完再交予Nginx返回给客户端. Nginx更 ...
随机推荐
- 开头什么的肯定要自我介绍然后把它扔到置顶咯>_<~
大家嚎,我是NanoApe~ 现在高一,是个OIer.音游狗和一个爱着二次元的萌汉子妹子,欢迎前来勾搭>_<~ 最近就是要冲省队啦~~~~加油! 扣扣号:879006461 Weibo:伪 ...
- [转]Linux下阅读源代码:(g)vim+Taglist+ctags
Linux下阅读源代码的方法很多,聪明人从标题应该就可以知道,需要(g)vim+Taglist+ctags.3者配合,真是珠联璧合,功力无限啊! vim/gvim什么是vim/gvim,如果看官连 ...
- code forces 994B
B. Knights of a Polygonal Table time limit per test 1 second memory limit per test 256 megabytes inp ...
- mysql5.6免安装配置(菜鸟版)
mysql5.6免安装配置 MySQL5.6.13安装步骤(Windows7 32位) 1. 下载MySQL Community Server 5.6.13 2. 解压MySQL压缩包 将以下载的My ...
- 优化web前端性能的几个方法
1 减少http请求, a. 合并脚本跟样式文件,如可以把多个 CSS 文件合成一个,把多个 JS 文件合成一个. b. CSS Sprites 利用 CSS background 相关元素进行背景图 ...
- mac 安装 visual studio 配置
前言:今天主要分享的是 Mac 下一款编程软件--Visual Studio,的确,这款软件之前一直是只有 Windows 系统独占的,2017年终于开发了 Mac 版本. 微软这次为 Mac 开发者 ...
- 转:Android ListView 异步加载图片
http://www.iteye.com/topic/1118828 http://www.iteye.com/topic/1127914 这样做无疑是非常可取的方法,但是加载图片时仍然会感觉到轻微的 ...
- 让Vs2010支持 Css3+HTML5
第一步. 先到微软官方下载一个 Microsoft Visual Studio 2010 sp1 . 给传送门:.microsoft.com/downloads/zh-cn/details.aspx? ...
- 生成一个空白BMP的简单代码【转】
转自:http://blog.chinaunix.net/uid-15063109-id-4275395.html 做图像处理时,有时需要临时生成图使用.以下是生成320x240 24位图的一个简单的 ...
- 关于多态的理解,有助于理解TStream抽象类的多态机制。
有的时候 不是很明白流的机制,因为有内存流 文件流 图片流 等等 他们之间的相互转化 靠的就是流的多态性.... unit Unit11; interface uses Winapi.Windows ...