Nginx设置不同路径/域名访问不同后端服务器

Nginx设置不同路径/域名访问不同后端服务器

 

 

示例nginx.conf

描述两种情况 通过rewrite访问和直接设置路径访问

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /usr/local/nginx/logs/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {

    include       mime.types;
    default_type  application/octet-stream;

    #access_log  logs/access.log  main;
    client_max_body_size 20m;

    sendfile        on;
    tcp_nopush     on;
    keepalive_timeout  65;

    gzip  on;

    proxy_cache_path ./web_cache levels=1:2 keys_zone=web_cache:1024m inactive=1000m max_size=1g;

    upstream manage {
        server 120.0.100.100:8000 weight=10  max_fails=3 fail_timeout=30s;
        server 120.0.100.100:8002 weight=10  max_fails=3 fail_timeout=30s;
    }

    upstream cwbase {
        server 127.0.0.1:32787 weight=10 max_fails=3 fail_timeout=30s;
        server 127.0.0.1:32788 weight=10 max_fails=3 fail_timeout=30s;
    }

server {
        listen       443 ssl;
        server_name  www.xxx.com;

        ssl_certificate /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/www.xxx.com/www.xxx.com.crt;
        ssl_certificate_key /root/.caddy/acme/acme-v02.api.letsencrypt.org/sites/www.xxx.com/www.xxx.com.key;
        ssl_session_timeout 5m;
        ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_prefer_server_ciphers on;

        root         /usr/share/nginx/html;

        # �~@�~P�gzip
        gzip on;

        # �~P��~T�gzip�~N~K缩�~Z~D�~\~@�~O�~V~G件�~L�~O�~N设置�~@��~Z~D�~V~G件�~F�~M�~Z�~N~K缩
        gzip_min_length 1k;

        # gzip �~N~K缩级�~H��~L1-9�~L�~U��~W�~J大�~N~K缩�~Z~D�~J好�~L�~_�~J�~M| �~T�CPU�~W��~W��~L�~P~N�~]��~Z�~\~I详�~F说�~X~N
        gzip_comp_level 3;

        # �~[�~L�~N~K缩�~Z~D�~V~G件类�~^~K�~@~Bjavascript�~\~I�~Z�~M形�~O�~@~B�~E�中�~Z~D�~@��~O�以�~\� mime.types �~V~G件中�~I��~H��~@~B
        gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png application/vnd.ms-fontobject font/ttf font/opentype font/x-woff image/svg+xml;

        # �~X��~P��~\�http header中添�~J| Vary: Accept-Encoding�~L建议�~@�~P�
        gzip_vary on;

        # �~A�~T�IE 6 gzip
        gzip_disable "MSIE [1-6]\.";

        # 设置�~N~K缩�~I~@�~\~@�~A�~Z~D�~S�~F��~L�大�~O
        gzip_buffers 32 4k;

        # 设置gzip�~N~K缩�~R~H对�~Z~DHTTP�~M~O议�~I~H�~\�
        gzip_http_version 1.0;

        # 这个配置是前端访问 https://www.xxx.com/api/ 真实访问地址为https://cwbase/api/
        location ^~ /api {

            rewrite ^/(.*) /$1 break;

            #add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'OPTION, POST, GET, DELETE, PUT';
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Content-Type';

            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

            proxy_pass http://cwbase/;

        }

        # 这个配置是前端访问 https://www.xxx.com/apis/ 真实访问地址为https://cwbase/api/
        # 后面的斜杠是一个关键,若无斜杠,传递到后端节点导致404
        location ^~ /apis/ {
            #后面的斜杠是一个关键,若无斜杠,传递到后端节点导致404
            #rewrite ^/(.*)$ /$1 break;
            add_header 'Access-Control-Allow-Credentials' 'true';
            add_header 'Access-Control-Allow-Methods' 'OPTION, POST, GET, DELETE, PUT';
            add_header 'Access-Control-Allow-Headers' 'X-Requested-With, Content-Type';

            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://cwbase/api/;

        }
    }
}

 

Nginx设置不同路径/域名访问不同后端服务器_第1张图片

 

Nginx设置不同路径/域名访问不同后端服务器_第2张图片

你可能感兴趣的:(nginx)