zoukankan      html  css  js  c++  java
  • nginx动静分离-copy

    1. 场景描述

    今天做nginx的动静分离,稍微走了点弯路,其实也谈不上,记录下吧,希望可以帮到有需要的朋友。

    2. 解决方案

    先说下我们的需求,使用域名(www.ruanjianlaowang.com)到服务器,服务器使用openresty部署的nginx,然后nginx做动静分离,静态文件按照模块分区;动态文件走负载均衡(走到springcloud gateway做用户统一认证后,再路由到对应系统),域名默认路由到首页。

    2.1 整体思路

    (1)首先是静态文件,在nginx的html文件中新建static文件夹,各个模块在static下面新建自己的文件夹;

    (2)除了静态文件,其余的都走动态代理,首先判断下是不是域名不带任何后缀,是的话,直接跳转到index.html;

    (3)动态请求跳转,负载均衡,跳转到网关进行校验。

    2.2 配置说明

    整体配置如下:

    #软件老王
    #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;
         upstream ruanjianlaowang{
           server 10.192.168.111:8002; 
           server 10.192.168.222:8002;
           server 10.192.168.333:8002;
         } 
        server {
            listen       80;
            server_name  ruanjianlaowang.com;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            #1 静态
            location /static {
                 root   /opt/openresty/nginx/html;
                 #index index.html;
            }     
            #2 动态
             location / {
              #3 首页
                  if ( $request_uri = "/" ) {
                  rewrite "/" http://ruanjianlaowang.com/static/index.html break;
                  }
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header X-Forwarded-Proto $scheme;
                 proxy_set_header Host $host:80;
                 proxy_set_header X-NginX-Proxy true;
                 proxy_set_header Connection "";
                 proxy_http_version 1.1;
                 #4负载均衡
                 proxy_pass http://ruanjianlaowang;
            } 
           underscores_in_headers on;
        }
    }
    #软件老王
    

    详细说明:

    (1) 静态请求

    当使用www.ruanjianlaowang.com/static进行请求的时候,首先精确匹配到/opt/openresty/nginx/html/static下面找对应的静态资源返回。

            #1 静态,软件老王
            location /static {
                 root   /opt/openresty/nginx/html;
                 #index index.html;
            } 
    

    (2)动态请求

    除去静态资源的精确匹配(/static),其他走动态资源请求。

      #2 动态,软件老王
             location / {
             }
    

    (3)首页跳转

    当请求中uri中只有域名的时候,转到index.html首页。

    #2 动态,软件老王
             location / {
              #3 首页,软件老王
                  if ( $request_uri = "/" ) {
                  rewrite "/" http://ruanjianlaowang.com/static/index.html break;
                  }
            } 
    

    (4)负载均衡,转网关

            #2 动态,软件老王
             location / {
              #3 首页,软件老王
                  if ( $request_uri = "/" ) {
                  rewrite "/" http://ruanjianlaowang.com/static/index.html break;
                  }
                 proxy_set_header X-Real-IP $remote_addr;
                 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                 proxy_set_header X-Forwarded-Proto $scheme;
                 proxy_set_header Host $host:80;
                 proxy_set_header X-NginX-Proxy true;
                 proxy_set_header Connection "";
                 proxy_http_version 1.1;
                 #4负载均衡,软件老王
                 proxy_pass http://ruanjianlaowang;
            }     
         upstream ruanjianlaowang{
           server 10.192.168.111:8002; 
           server 10.192.168.222:8002;
           server 10.192.168.333:8002;
         } 
  • 相关阅读:
    第八周总结
    第五周学习总结&实验报告(三)
    第四周课程总结&试验报告(二)
    第三周课程总结&实验报告(一)
    第二周Java学习总结
    2019春学习总结
    第二周基础学习
    第三周编程总结
    2019春第四次课程设计实验报告
    2019春第三次课程设计实验报告
  • 原文地址:https://www.cnblogs.com/hanease/p/14515292.html
Copyright © 2011-2022 走看看