微信小程序下nginx代理wss,實現兼容原本服務協議ws,Java版本 原 薦

開始前說明

微信小程序如果使用webSocket協議的話,那麼按照官網上的要求是必須使用了wss協議,使用了一個框架,但不支持wss,所以後面用nginx代理解決了這個問題,接下來上代碼####

軟件列表

  1. 我使用的是 nginx-1.12.1,附上下載地址http://nginx.org/,

詳細的nginx教程可以看一下這位的博客 http://www.cnblogs.com/edward2013/p/5506588.html

  1. 需要有SSL證書的域名一個

  2. 我使用的是tomcat7JDK7

  3. 服務器一個(有公網IP的那種,別問我爲什麼強調這個。。。)

熟悉這些的直接ctrl+f搜索關鍵字 '全文最主要',看着一步就好了

好了開始搞事情

  1. 首先先在服務器上部署你的項目,並且測試運行起來(保證外網訪問的到,通過公網IP訪問) 在tomcat7上運行項目,我去掉項目名,直接是ROOT文件夾 像這樣 輸入圖片說明 輸入圖片說明
  2. 在服務器上部署nginx,然後修改配置,然後測試跑起來 配置這樣修改,listen是監聽的端口不一定要80,如果衝突可以是別的端口 輸入圖片說明

然後運行起來(我是用域名綁定了公網IP,然後測試的),如圖效果,證明OK

輸入圖片說明

###3.(熟悉的直接看着一步就好)這一步nginx配置是全文最主要的了### 輸入圖片說明

可能需要的是這一步的代碼,沒有優化的nginx


#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    #nginx代理wss測試
    map $http_upgrade $connection_upgrade {  
    	default upgrade;  
    	'' close;  
    }

    upstream websocket {
	server 公網IP:9321;
    }

    server {
        listen       443 ssl;
        server_name  SSL域名;
	
	ssl on;
	# SSL 驗證配置
	ssl_certificate 證書.crt;#這裏的證書的別加上"",好像加上會變成絕對路徑,我是直接方法nginx下的conf裏面的
	ssl_certificate_key 證書.key;
	# 默認值
	ssl_ciphers HIGH:!aNULL:!MD5;
	ssl_session_timeout 10m; 
	ssl_protocols TLSv1.2 TLSv1.1 TLSv1; 
	ssl_prefer_server_ciphers on; 
	ssl_session_cache shared:SSL:10m;


        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
	    proxy_pass http://websocket;#代理到上面的地址去 
	    proxy_http_version 1.1; 
	    proxy_set_header Upgrade $http_upgrade; #這個上面有配置的你可以看一下
	    proxy_set_header Connection “Upgrade”; 
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}

###運行,我搞定了###

輸入圖片說明 輸入圖片說明

寫的不好請多指教,只供參考而已

如果想看nginx下怎麼保持webSocket長連接不被中斷,可以看一下我這篇

地址: 點擊跳轉

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章