Nginx反向代理Nodejs服务通过SSL证书实现微信小程序Post

安装Nginx,Nodejs 安装过程

启动Nodejs服务。express 或者别的什么后台服务

比如启动以后的服务地址 localhost:3000

配置Nginx

/etc/nginx/nginx.conf

server {
        listen       80 default_server;
        listen       [::]:80 default_server;
	listen	443 ssl;
	ssl_certificate  /etc/nginx/ssl/xxxx.crt;        #你的CRT
	ssl_certificate_key /etc/nginx/ssl/xxxx.key; #你的Key
	server_name xxx.com;                                   #域名
	ssl_session_cache shared:SSL:1m;
    	ssl_session_timeout 5m;
    	ssl_protocols SSLv3 TLSv1;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
		proxy_pass http://127.0.0.1:3000;        #Node的服务
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        	proxy_set_header Host $host;
        	proxy_set_header X-Forward-Proto https;
        	proxy_http_version 1.1;
        	# for websocket
        	proxy_set_header Upgrade $http_upgrade;
        	proxy_set_header Connection "upgrade";
        }

然后重启Nginx
sudo systemctl restart nginx

在小程序里Post
wx.request({
url: “https://xxx.com:443/posts”,
…….
OK~

PS:上面是腾讯云的设置,阿里云的略有所不同。

Centos 7 安装 Nginx

Step One—Add Nginx Repository

第一步,添加Nginx 库

sudo yum install epel-release

Step Two—Install Nginx

第二步安装

sudo yum install nginx

输入Y

Step Three—Start Nginx

第三步运行

Nginx does not start on its own. To get Nginx running, type:

sudo systemctl start nginx

如果没什么问题就可以输入网站地址测试。成功的话就见到下面的的图。

因为我同时安装了Apache,所以80端口被占用了。启动的时候报错

Unit start.service could not be found.
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since 三 2018-01-17 18:34:14 CST; 10s ago
  Process: 4237 ExecStart=/usr/sbin/nginx (code=exited, status=1/FAILURE)
  Process: 4234 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 4233 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)

1月 17 18:34:12 VM_66_141_centos nginx[4237]: nginx: [emerg] bind() to 0.0....)
1月 17 18:34:12 VM_66_141_centos nginx[4237]: nginx: [emerg] bind() to 0.0....)
1月 17 18:34:13 VM_66_141_centos nginx[4237]: nginx: [emerg] bind() to 0.0....)
1月 17 18:34:13 VM_66_141_centos nginx[4237]: nginx: [emerg] bind() to 0.0....)
1月 17 18:34:14 VM_66_141_centos nginx[4237]: nginx: [emerg] bind() to 0.0....)
1月 17 18:34:14 VM_66_141_centos systemd[1]: nginx.service: control process...1
1月 17 18:34:14 VM_66_141_centos nginx[4237]: nginx: [emerg] still could no...)
1月 17 18:34:14 VM_66_141_centos systemd[1]: Failed to start The nginx HTTP....
1月 17 18:34:14 VM_66_141_centos systemd[1]: Unit nginx.service entered fai....
1月 17 18:34:14 VM_66_141_centos systemd[1]: nginx.service failed.
Hint: Some lines were ellipsized, use -l to show in full.

这个时候修改配置文件/etc/nginx/nginx.conf

server {
        listen       8088 default_server;
        listen       [::]:8088 default_server;

然后执行下面命令测试一下

nginx -t -c /etc/nginx/nginx.conf

执行下面命令加入启动项

sudo systemctl enable nginx

Over.