Nginx动静分离的几种方式

2023年 7月 15日 58.5k 0

这篇文章简单的介绍nginx proxy_pass和一些配置。仅作为参考使用。请输入图片描述proxy_pass属于ngx_http_proxy_module,此模块可以将请求发送到一台服务器上。proxy_pass放在location标签中,可使用http。如下:

location /poy{
   proxy_pass http://10.0.0.90;
}

参数:proxy_set_header,(当后端web服务器上有多个虚拟主机,需要使用header来区分代理的主机名)获取用户的主机名或者真是IP,以及代理者的IP地址

server {
     listen       80;
     server_name  www.linuea123.com;
     location / {
         root   html;
         index  index.html index.htm;
     proxy_pass http://backend;
     proxy_set_header Host $host;
     }

获取用户的真是iP,截获客户请求地址(nginx开启即可,appche需要在LogFormat ""%{X-Forwareded-for}i添加)proxy_set_header Host $host;proxy_set_header X-Forwarded-For $remote_addr 获取用户的真是iP,截获客户请求地址client_body_buffer_size:用于指导客户端请求主题缓冲区大小,用户访问先保存到本地在传递给用户proxy_connect_timeout:表示与后端服务器连接的超时时间,既发起握手等待响应的超时时间proxy_send_timeoit:表示后端服务数据返回最短时间,超过则断开proxy_read_timeout:设置nginx从代理的后端服务器获取信息proxy_buffer_size:设置缓存区大小,默认缓冲大小指令:proxy_buffers设置

location /poy{
           proxy_pass http://10.0.0.90;
            proxy_set_header Host $host;
}

proxy.conf如下:

proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

目录和扩展名分离测试(还有浏览器的分离):

upstream jingtai {
    server 10.0.1.10:80 weight=5 max_fails=10 fail_timeout=10s;
    server 10.0.1.11:80 weight=5 max_fails=10 fail_timeout=10s;
}
upstream dongtai {
    server 10.0.2.10:80 weight=5 max_fails=10 fail_timeout=10s;
    server 10.0.2.11:80 weight=5 max_fails=10 fail_timeout=10s;
}
server {
    listen 80;
    server_name www.linuxea.com;
    location / {
        root html;
        index index.html index.htm;
        proxy_pass http://dongtai;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
}        
    location /image/ {
        proxy_pass http://jingtai;

扩展名分离

location ~ .* (gif|jpg|jpeg|png|bmp|swf|css|js)$ {
    proxy_pass http://jingtai;
    include proxy.conf;
}
location ~ .*. (php|php3|php5.3)$ {
    proxy_pass httpd://jingtai;
    include proxy.conf;    
}

if

if($request_uri ~* ".*.(jsp|jsp*|do|do*)$")
{
proxy_pas httpd://jingtai;
}

android-iphone

listen 80;
server_name app.linuxea.com;    
location / {
    if (http_user_agent ~* "android")
 {
proxy_pass http://android;
}
   if (http_user_agent ~* "iphone")
{
   proxu_pass http:/iphone;
}
   proxy_pass http://pc;
   include proxy.conf;
}

proxy如下

proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
}
location /dongtai/ {
    proxy_pass http://dongtai;
proxy_redirect off;
        proxy_set_header Host $host;
        p2roxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_connect_timeout 90;
        proxy_send_timeout 90;
        proxy_read_timeout 90;
        proxy_buffer_size 4k;
        proxy_buffers 4 32k;
        proxy_busy_buffers_size 64k;
        proxy_temp_file_write_size 64k;
}
}

proxy参数proxy_set_header Host $host; 用户请求的header传递给后端访问p2roxy_set_header X-Real-IP $remote_addr;接收客户端ip,通过X-forwared给后端proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;代理和web超时响应参数如下:

proxy_connect_timeout 60;
proxy_send_timeout 30;
proxy_read_timeout 30;

内存和磁盘缓冲参数如下:

proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

相关文章

LeaferJS 1.0 重磅发布:强悍的前端 Canvas 渲染引擎
10分钟搞定支持通配符的永久有效免费HTTPS证书
300 多个 Microsoft Excel 快捷方式
一步步配置基于kubeadmin的kubevip高可用
istio全链路传递cookie和header灰度
REST Web 服务版本控制

发布评论