LoginSignup
5
3

More than 5 years have passed since last update.

Nginx 1.10.1でHTTP/2, TLS 1.2をつかっているとiOSからPOSTリクエストができない!?

Posted at

前提条件

  • Ubuntu 16.04 LTS
  • Python 2.7.11
  • Nginx 1.10.1
  • 小学生に怒られる覚悟

環境

View <-> Nginx <-> gunicorn/Django

みたいな関係でアプリケーションが動いている

下手な説明をするとDjango自体は gunicorn PROJECT_NAME.wsgi:application --bind 0.0.0.0:8000 -D で起動し、Nginxでリバースプロキシでhogehogeっていう感じ

エラー内容

Xcodeのコンソールに出てくるログでは以下のように出てくる

FAILURE: Error Domain=NSURLErrorDomain Code=-1004 "Could not connect to the server." UserInfo={NSUnderlyingError=0x137d39380 {Error Domain=kCFErrorDomainCFNetwork Code=-1004 "(null)" UserInfo={NSErrorPeerAddressKey={length = 16, capacity = 16, bytes = 0x100201bb341a9f540000000000000000}, _kCFStreamErrorCodeKey=-2200, _kCFStreamErrorDomainKey=4}}, NSErrorFailingURLStringKey=[FILTERED], NSErrorFailingURLKey=[FILTERED], _kCFStreamErrorDomainKey=4, _kCFStreamErrorCodeKey=-2200, NSLocalizedDescription=Could not connect to the server.}

引用: http://stackoverflow.com/questions/36907767/nsurlerrordomain-code-1004-for-few-seconds-after-app-start-up

(エラーログを控え忘れた)

どういうことだ

スクリーンショット 2016-10-15 午後3.44.19.png

どうやら上記引用先の回答をみると Nginx 1.10.0 (and 1.9.15) 。つまりNginxがHTTP/2に対応したバージョンからHTTP/2を使いつつTLS 1.2をサポートしていると Could not connect to the server. というようなエラーが出されてしまうようだ。

対策

Nginxを2016/10/15現在最新版のMainlineの1.11.5にバージョンアップをしてあげればいい

スクリーンショット 2016-10-15 午後3.44.25.png

ちょっと下の回答をみると http2_body_prepared_size を更に指定してあげる必要があるらしい

*) Change: HTTP/2 clients can now start sending request body
immediately; the "http2_body_preread_size" directive controls size of
the buffer used before nginx will start reading client request body.

引用: http://nginx.org/en/CHANGES

公式さんのアップデート情報をみるとこんな感じなのでやっぱり1.11.5が必要

Nginxアップデート

codename のところは各自変更で。また、Debian, Ubuntu前提

$ echo "deb http://nginx.org/packages/mainline/ubuntu/ codename nginx" > /etc/apt/sources.list
$ echo "deb-src http://nginx.org/packages/mainline/ubuntu/ codename nginx" > /etc/apt/sources.list
$ sudo apt-get update
$ sudo apt-get upgrade -y
$ nginx -v
nginx version: nginx/1.11.5

設定ファイル

nginx.conf
server {
    ・・・
    http2_body_preread_size 64k;
    ・・・
}

上記のような形で付け加える。

適用

# 一応テスト
$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
$ sudo /etc/init.d/nginx restart
[ ok ] Restarting nginx (via systemctl): nginx.service.

一応今回のサーバで設定してるファイルは以下
https://github.com/nnsnodnb/django-mbaas/blob/master/nginx.conf

5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3