使用nginx反向代理,一個80端口下,配置多個微信項目,nginx80
使用nginx反向代理,一個80端口下,配置多個微信項目,nginx80
我們要接入微信公眾號平台開發,需要填寫服務器配置,然後依據接口文檔才能實現業務邏輯。但是微信公眾號接口只支持80接口(80端口)。我們因業務需求需要在一個公眾號域名下面,發布兩個需要微信授權的項目,怎麼辦?
我們可以用nginx服務器做反向代理來解決這個問題。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 Settings
27 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端口。所以兩個項目可以同時實現微信授權登錄等。
http://xxxxxx/Linuxjc/1184871.html TechArticle