使用nginx反向 Proxy,一個80連接埠下,配置多重專案,nginx80
我們要接入公眾號平台開發,需要填寫伺服器配置,然後依據介面文檔才能實現商務邏輯。但是公眾號介面只支援80介面(80連接埠)。我們因業務需求需要在一個公眾號網域名稱下面,發布兩個需要授權的項目,怎麼辦?
我們可以用nginx伺服器做反向 Proxy來解決這個問題。nginx伺服器對外80連接埠,然後根據URL參數不同,對內訪問不同的項目。
nginx配置如下:
開啟/usr/local/nginx/conf/nginx.conf
1 worker_processes 4; 2 error_log logs/error.log; 3 events { 4 worker_connections 1024; 5 } 6 http { 7 include mime.types; 8 default_type application/octet-stream; 9 sendfile on;10 keepalive_timeout 65;11 12 gzip on;13 gzip_min_length 1k;14 gzip_buffers 16 64k;15 gzip_http_version 1.1;16 gzip_comp_level 6;17 gzip_types text/plain application/x-javascript text/css application/xml application/javascript;18 gzip_vary on;19 20 #指向項目一21 upstream backend1 {22 server 192.168.1:8081;23 }24 #指向項目二25 upstream backend2{26 192.168.1.1:8082;27 }28 proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:128m inactive=1d max_size=1G;29 include vhosts/*;30 }View Code
開啟/usr/local/reverse_proxy_nginx/conf/nginx.conf
1 worker_processes 2; 2 events { 3 worker_connections 1024; 4 } 5 http { 6 include mime.types; 7 default_type application/octet-stream; 8 access_log /home/nginx_log/reverse_proxy_no1_access.log; 9 sendfile on;10 keepalive_timeout 65;11 upstream backend1 {12 #server 192.168.1.1:8181;13 server 192.168.1.1:8081;14 }15 upstream backend2 {16 #server 192.168.1.1:8082;17 server 192.168.1.1:8082;18 }19 proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache:128m inactive=30m max_size=1G;20 server {21 listen 8081;22 server_name h5.xxxx.com;23 24 location / {25 proxy_pass http://backend1;26 #Proxy Settings27 proxy_redirect off;28 proxy_set_header Host $host;29 proxy_set_header X-Real-IP $remote_addr;30 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;31 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;32 proxy_max_temp_file_size 0;33 proxy_connect_timeout 90;34 proxy_send_timeout 90;35 proxy_read_timeout 90;36 proxy_buffer_size 4k;37 proxy_buffers 4 32k;38 proxy_busy_buffers_size 64k;39 proxy_temp_file_write_size 64k;40 add_header Nginx-Res "http://backend1";41 }42 43 location ~ ^/(h5)(.*)$ { 44 proxy_pass http://backend2;45 proxy_redirect off;46 proxy_set_header Host $host;47 proxy_cache cache;48 proxy_cache_valid 200 302 1d;49 proxy_cache_valid 301 1d;50 proxy_cache_valid any 1m;51 expires 1h;52 add_header Nginx-Res "http://backend2";53 proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";54 add_header Nginx-Cache "$upstream_cache_status";55 }56 57 58 error_page 500 502 503 504 /50x.html;59 location = /50x.html {60 root html;61 }62 location ~ .*\.(gif|jpg|png|css|js|ico)(.*) {63 proxy_pass http://backend1;64 proxy_redirect off;65 proxy_set_header Host $host;66 proxy_cache cache;67 proxy_cache_valid 200 302 30d;68 proxy_cache_valid 301 1d;69 proxy_cache_valid any 1m;70 expires 30d;71 proxy_ignore_headers "Cache-Control" "Expires" "Set-Cookie";72 add_header Nginx-Res "http://backend1";73 add_header Nginx-Cache "$upstream_cache_status";74 }View Code
當我們開啟URL包含h5時,就會跳到8081連接埠項目中,但是對外還是80連接埠。所以兩個項目可以同時實現授權登入等。