page contents

关于Nginx proxy_pass转发问题

Pack 发布于 2020-02-13 21:13
阅读 862
收藏 0
分类:服务器

关于nginx proxy_pass的问题,以下是我的nginx配置,我想要指定/api/wxapp/order/createOrder地址,被转发到https://www.aaa.com去处理,并想服务器自身不再处理这个地址(也就是16和19两台服务器不再接受该地址)。
但实际上16,19两台服务器和www.aaa.com的服务器都会处理? 请问配置哪里有问题吗?

upstream jdshop {
           ip_hash;
	   server 172.24.24.16:8080    weight=8  max_fails=2 fail_timeout=20s;
           server 172.24.24.19:8080    weight=7  max_fails=2 fail_timeout=20s;
         }


server {
    listen       443;
    server_name  haohuo.aaa.com;
    if ($http_host !~ "^haohuo.aaa.com$")
    {
    	rewrite ^(.*) https://haohuo.aaa.com$1 permanent;
    }
    charset utf-8;
    access_log  /var/log/nginx/jdhh.access.log  main;
    #ssl配置
    ssl on;
    ssl_certificate  /etc/nginx/cert/2503249_haohuo.aaa.com.pem;
    ssl_certificate_key /etc/nginx/cert/2503249_haohuo.aaa.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;

    #代理配置参数
    proxy_connect_timeout 180;
    proxy_send_timeout 180;
    proxy_read_timeout 180;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarder-For $remote_addr;
    client_max_body_size        30m; #文件最大大小
    underscores_in_headers on;  

    location / {
        mirror /mirror;
        proxy_pass http://jdshop;
    }

    location /mirror {
        internal;
        proxy_pass https://www.aaa.com/api/mirror/copy;
        proxy_set_header X-Original-URI $request_uri;
    }

    location /api/wxapp/order/createOrder {
        proxy_pass https://www.aaa.com;
    }

    #location ~*/api/wxapp/coupon/chooseCouponList~* {
    #    proxy_pass http://172.24.24.20:8080;
    #}

    location ~/(api|admin|article|business|cart|common|consultation|friend_link|member|order|password|payment|product|product_category|product_notify|promotion|resources|review|sitemap|social_user_login|store|system|cert|upload|tempImage)/ { 
	    mirror /mirror;
        proxy_pass http://jdshop;
    }

    location ~/(favicon.ico|lUwcESlWwA.txt|NWgPy1Gwrm.txt|robots.txt) {
    	#proxy_pass http://localhost:8080;
        proxy_pass http://jdshop;
	
	#if ($http_shenghe = "2") { 
	#  	proxy_pass http://172.24.24.20:8081;  
	# }	
    }



    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


    location /images/ {
         root  /data/deploy/website/static_file;              

    }



    location /upload/ {
         root  /data/deploy/website/jdshop;

    }
}

345
Pack
Pack

你这个有点长了,可以在nginx的conf目录下新建一个自己的配置文件,然后在这个目录下单独写一个.conf文件,然后把你需要代理的端口和地址写在这个.conf文件中,最后把nginx.conf的最后面加一行你刚刚写的.config的路径比如: include proxyconf/xxx.conf;,这个proxyconf就是新建的一个文件夹,xxx.conf就是你需要加的具体配置。
xxx.conf内容如下:

TODO SHINE CONFIG

/mnt/fed-projects/Shine-Standard/dist

server {
listen 代理的端口;
root /home/admin/ability/dist; # 代理的前端的地址

    location /api/wxapp/order/createOrder/ {  # 这里的地址可以写,也可以不写
            proxy_read_timeout 300; # Some requests take more than 30 seconds.
            proxy_connect_timeout 300; # Some requests take more than 30 seconds.
            proxy_set_header   X-Forwarded-Proto $scheme;
            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_set_header   X-Forwarded-Ssl   on;
            proxy_set_header   X-Frame-Options   SAMEORIGIN;

            proxy_pass http://172.24.24.16:8080/api/wxapp/order/createOrder; #
    }

    location /api/wxapp/order/createOrder/ { # 这里的地址可以写,也可以不写
            proxy_read_timeout 300; # Some requests take more than 30 seconds.
            proxy_connect_timeout 300; # Some requests take more than 30 seconds.
            proxy_set_header   X-Forwarded-Proto $scheme;
            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_set_header   X-Forwarded-Ssl   on;
            proxy_set_header   X-Frame-Options   SAMEORIGIN;

            proxy_pass http://172.24.24.19:8080/api/wxapp/order/createOrder/;
    }
    index    index.html  index.htm  index.cgi  index.php  index.php5 ;

}
访问的时候就是当前部署nginx的这台机器+代理的端口,如果要通过https://www.aaa.com访问则需要将当前部署nginx的ip和代理端口做域名映射。我觉得是这么个回事儿,可以试一试!

请先 登录 后评论