Nginx反向代理一個80端口下配置多個微信項目詳解
我們要接入微信公眾號平臺開發,需要填寫服務器配置,然后依據接口文檔才能實現業務邏輯。但是微信公眾號接口只支持80接口(80端口)。我們因業務需求需要在一個公眾號域名下面,發布兩個需要微信授權的項目,怎么辦?
我們可以用nginx服務器做反向代理來解決這個問題。nginx服務器對外80端口,然后根據URL參數不同,對內訪問不同的項目。
nginx配置如下:
打開/usr/local/nginx/conf/nginx.conf
worker_processes 4;error_log logs/error.log;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65;gzip on;gzip_min_length 1k;gzip_buffers 16 64k;gzip_http_version 1.1;gzip_comp_level 6;gzip_types text/plain application/x-javascript text/css application/xml application/javascript;gzip_vary on; #指向項目一 upstream backend1 { server 192.168.1:8081; } #指向項目二 upstream backend2{ 192.168.1.1:8082; } proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:128m inactive=1d max_size=1G; include vhosts/*;}
打開/usr/local/reverse_proxy_nginx/conf/nginx.conf
worker_processes 2;events { worker_connections 1024;}http { include mime.types; default_type application/octet-stream; access_log /home/nginx_log/reverse_proxy_no1_access.log; sendfile on; keepalive_timeout 65; upstream backend1 { #server 192.168.1.1:8181; server 192.168.1.1:8081; } upstream backend2 { #server 192.168.1.1:8082; server 192.168.1.1:8082; } proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:128m inactive=30m max_size=1G; server { listen 8081; server_name h5.xxxx.com; location / { proxy_pass http://backend1; #Proxy Settings 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_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_max_temp_file_size 0; 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; add_header Nginx-Res "http://backend1"; } location ~ ^/(h5)(.*)$ { proxy_pass http://backend2; proxy_redirect off; proxy_set_header Host $host; proxy_cache cache; proxy_cache_valid 200 302 1d; proxy_cache_valid 301 1d; proxy_cache_valid any 1m; expires 1h; add_header Nginx-Res "http://backend2"; proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; add_header Nginx-Cache "$upstream_cache_status"; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~ .*/.(gif|jpg|png|css|js|ico)(.*) { proxy_pass http://backend1; proxy_redirect off; proxy_set_header Host $host; proxy_cache cache; proxy_cache_valid 200 302 30d; proxy_cache_valid 301 1d; proxy_cache_valid any 1m; expires 30d; proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie"; add_header Nginx-Res "http://backend1"; add_header Nginx-Cache "$upstream_cache_status"; }
當我們打開URL包含h5時,就會跳到8081端口項目中,但是對外還是80端口。所以兩個項目可以同時實現微信授權登錄等。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
新聞熱點
疑難解答