受欢迎的博客标签

Wechat-微信服务号Asp .Net Core开发实战记录(9)-开发者服务器配置nginx反向代理,服务

Published

出现的问题:

1.系统环境

os:阿里云ubuntu18.04 *64

nginx: 1.14.0

2.实施步骤

2.1.1 直接用下面的方式测试,开发者服务器能否收到微信官方服务器post过来的消息

dotnet run --urls http://0.0.0.0:80

2.1.2开发者服务器认证:进入微信公众平台,选择“修改配置”,然后点击“提交”,从控制台日志会看开发者服务器收到微信官方服务器发起的get请求。如果一切顺利,微信公众平台会显示“提交成功”的弹窗提示。否则会提示认证失败的原因。

2.1.3 微信官方服务器发起的Post请求:微信官方收发消息都正常

2.2 nginx配置

2.2.1 遇到的问题(没收到微信官方服务器post过来的消息)

用nginx反向代理后,http get方式正常,但未收到官方服务器post过来的消息。经查nginx日志,官方微信服务器Post过来消息,但nginx返回400,未转发到开发者服务器

 

post 请求 变成了 get请求

如果采用http进行请求,可能造成重定向到https,这是可能就从post请求,变成了get请求,所以才报类型错误
You may also want to try a 307 redirect. I found that my POST's were turning into GET's with the 301 redirect.

http://weixin.usdotnet.com/weixin?originalid=gh_5588defc6e31
change http:// to  https://
https://weixin.usdotnet.com/weixin?originalid=gh_5588defc6e31

 

2. localhost:9000没收到post请求

问题:nginx 配置 connetion:“upgrade”

方法:trace 级别跟踪,会打印出错误

 

2.2.2 采用http 协议新建server 80端口

应为转发配置参数造成的,用以下参数转发

/ect/nginx/sites-enabled/default

server {

                  
                listen 80 ;
	#listen [::]:80 ;

                 server_name weixin.usdotnet.com;
                                              access_log  /var/log/nginx/weixin.usdotnet.com.access.log;



        location / { 
	   proxy_pass  http://localhost:9000;
           proxy_set_header Host $host;
           proxy_set_header X_Real_IP $remote_addr;
           proxy_set_header X-Forwarded $proxy_add_x_forwarded_for;
         }    
	  #return 404; # managed by Certbot
}

 

服务启动配置

sudo vi /etc/systemd/system/kestrel-weixinusdotnetcom.service

[Unit]
Description=Example .NET Web API App running on Ubuntu

[Service]
WorkingDirectory=/var/www/weixin/WanJiaLe_0736_HunanChangDe/src/Presentation/WeiXinThirdServer
ExecStart=/usr/bin/dotnet --urls http://localhost:9000
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

 


Restart=on-failure

 

sudo systemctl enable kestrel-weixinusdotnetcom.service
sudo systemctl start kestrel-weixinusdotnetcom.service
sudo systemctl stop kestrel-weixinusdotnetcom.service
sudo systemctl restart kestrel-weixinusdotnetcom.service
sudo systemctl status kestrel-weixinusdotnetcom.service

sudo journalctl -fu kestrel-weixinusdotnetcom.service

sudo systemctl daemon-reload 

 

 

数据库配置

use JiaLe
db.createUser({user:"JiaLe", pwd:"***", roles:[{role:"dbOwner", db:"JiaLe"}]})

 

服务器启动