title: Nginx的反向代理和动静分离
date: 2019-05-26
tags: nginx
Nginx的反向代理和动静分离
以我的项目为例,展示反向代理和动静分离的过程,具体概念自行百度
每个人的项目情况不同,我在摸索两者的过程中搜索了很多相关的东西,类似的可以参考,但是不能照搬,不然会发生莫名其妙的错误
首先安装nginx
这个写的挺好 https://www.cnblogs.com/taiyonghai/p/6728707.html
我在这里tomcat端口是80 nginx端口改成了8090
并把服务ip改成了阿里云服务器上的公网ip
浏览器访问http://47.100.161.71:8090/后有:
这说明nginx安装和部署成功
接着进行反向代理,只是演示代理tomcat服务器(这里的前提是笔者已经在阿里云服务器上安装好tomcat并把项目跑起来了),反向代理把tomcat的请求交给nginx去处理,
打开nginx配置文件 nginx.conf
添加upstream模块起名lagoon(名字随意)
upstream lagoon{
server 47.100.161.71:80;
}
设定server 为阿里云公网ip+tomcat端口号
接着为nginx设置请求路径
location /tmall {
# root html;
# index index.html index.htm;
proxy_pass http://lagoon/tmall;
}
这个意思是当nginx捕捉到请求路径/tmall时,把他跳转到http://lagoon/tmall
也就是tomcat上能够运行起来的项目主页路径
其中lagoon就是 47.100.161.71:80
保存配置文件并重启nginx(方法百度)
如下,这是tomcat上运行的项目
回车:
进入了项目主页
接着试试nginx代理后的效果
回车:
同样进入了项目主页,简单的说就是nginx捕捉到/tmall请求,把它跳转到tomcat里去展示,但是同时又是自己的路径地址。
这是一种很简单的代理
有关动静分离
让图片资源,css js等在tomcat里加载效率比较低
我们让nginx处理这些静态资源,当然动态的还是tomcat来做
一个流程是:当该项目有请求的静态资源时,不用tomcat去联网加载,而是用服务器本地的静态资源去展示
这个方法很多博客上说到过,不过仅仅是用nginx来搭建图片服务器而言,不适用于我的项目场景
一开始尝试了很多配置之后,用谷歌浏览器右键审查图片加载状况,一开始发现403,资源请求被拒绝,这个时候把nginx配置里的用户设置为root,
使得nginx具有最高权限去工作
接着发现项目里的图片还是出不来,审查为404
类似以上这种后缀匹配不适用于我的项目场景,经审查元素和console后,我发现当前项目图片的请求路径是
http://47.100.161.71:8090/tmall/img/productSingle_small/1322.jpg
这种形式的
也就是如图(演示例子,当时是出不来的)
类比请求路径/tmall
我做了以下配置文件修改
意思为如果请求路径为/tmall/img/时
让nginx拿本地存在的静态图片去显示
看路径应该就知道服务器本地存放的静态图片就是部署在tomcat webapps下面的项目的图片存放文件夹
alias的用法自行百度
同理css js的用法一样
这样就做到了静态资源不用tomcat去在容器里加载,这样速度也慢,效率也低
而是让所有的静态资源请求交给nginx,让nginx从本地存在的静态资源里拿出去给大家显示
下面是配置文件的所有内容
user root;
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;
upstream lagoon{
server 47.100.161.71:80;
}
server {
listen 8090;
server_name 47.100.161.71;
#charset koi8-r;
#access_log logs/host.access.log main;
location /tmall {
# root html;
# index index.html index.htm;
proxy_pass http://lagoon/tmall;
}
location /tmall/img/ {
alias /usr/local/tomcat/apache-tomcat-8.5.41/webapps/tmall/img/;
autoindex on;
}
#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;
# }
#}
}
最后做好相关修改后重启nginx访问项目,审查图片来源头部信息(图片加载会快一些了~)
会发现图片是通过nginx来把关显示的
有关nginx的反向代理和动静分离还有许多需要深入的地方,还有缓存策略等等。
还是需要对nginx进行一个全方位的学习和了解的