1 參考文檔
http://nginx.org/en/download.html
http://nginx.org/en/docs/configure.html
https://github.com/openresty/lua-nginx-module
http://nginx.org/en/docs/http/configuring_https_servers.html
https://www.openssl.org/docs/man1.1.0/apps/genrsa.html
2 安裝
2.1 打開防火牆端口
$ sudo /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT $ sudo /sbin/iptables -I INPUT -p tcp --dport 8443 -j ACCEPT $ sudo service iptables save
2.2 獲取安裝包以及相關依賴
# 支持https $ wget https://www.openssl.org/source/openssl-1.0.2m.tar.gz $ wget https://ftp.pcre.org/pub/pcre/pcre-8.41.tar.bz2 $ wget http://zlib.net/zlib-1.2.11.tar.gz $ wget http://nginx.org/download/nginx-1.12.2.tar.gz # 支持lua $ wget https://github.com/simpl/ngx_devel_kit/archive/v0.3.0.tar.gz $ wget https://github.com/openresty/lua-nginx-module/archive/v0.10.11.tar.gz
2.3 解壓安裝包
$ tar zxvf openssl-1.0.2m.tar.gz $ tar jxvf pcre-8.41.tar.bz2 $ tar zxvf zlib-1.2.11.tar.gz $ tar zxvf v0.3.0.tar.gz $ tar zxvf v0.10.11.tar.gz $ tar zxvf nginx-1.12.2.tar.gz && cd nginx-1.12.2
2.4 安裝nginx
注意:添加LUA支持,需要安裝lua環境,參見LUA-環境搭建
$ ./configure --prefix=/opt/jediz90/nginx --with-pcre=../pcre-8.41 --with-zlib=../zlib-1.2.11 \ --with-openssl=../openssl-1.0.2m --with-http_gzip_static_module \ --with-http_stub_status_module --with-http_ssl_module # 添加TCP反向代理支持 --with-stream # 添加HTTP2支持 --with-http_v2_module # 添加LUA支持,需要安裝lua環境--with-ld-opt="-Wl,-rpath,/opt/sloth/lj2/lib" \ --add-module=/opt/sloth/ngx_devel_kit-0.3.0 \ --add-module=/opt/sloth/lua-nginx-module-0.10.11
$ make && make install
2.5 添加開機啟動
$ sudo vim /lib/systemd/system/nginx.service
在文件中添加以下內容
[Unit] Description=nginx After=network.target [Service] User=sloth Group=sloth Type=forking ExecStart=/opt/jediz90/nginx/sbin/nginx ExecReload=/opt/jediz90/nginx/sbin/nginx -s reload ExecStop=/opt/jediz90/sbin/nginx -s quit PrivateTmp=true [Install] WantedBy=multi-user.target
添加到開機啟動
$ sudo systemctl enable nginx.service
3 LUA配置
編輯nginx.conf文件
location /lua { default_type 'text/plain'; content_by_lua_block { ngx.say("dog") } }
4 HTTS配置
$ mkdir /opt/jediz90/nginx/conf/key && cd /opt/jediz90/nginx/conf/key
4.1 Openssl 生成https證書
注:需要先安裝openssl
# 安裝openssl,如之前已經解壓則無需在解壓一遍 $ tar zxvf openssl-1.0.2m.tar.gz $ cd openssl-1.0.2m $ ./config --prefix=/opt/jediz90/openssl $ make && make install $ cd .. && rm -rf openssl-1.0.2m
創建私鑰
$ /opt/jediz90/openssl/bin/openssl genrsa -out jediz90.key 1024
生成自簽證書
$ openssl req -new -x509 -days 3650 -key jediz90.key -out jediz90.crt
編輯nginx.conf文件
$ vi /opt/jediz90/nginx/conf/nginx.conf
添加以下內容
server { listen 8443 ssl; server_name localhost; ssl_certificate key/jediz90.crt; ssl_certificate_key key/jediz90.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; } }
5 HTTP2配置
參考上面HTTPS配置,然后進行以下調整
listen 8443 ssl http2;
6 解決 Mac OS X 下 Nginx 編譯報錯 symbol(s) not found for architecture x86_64
./configure 命令后, 不要繼續 make, 要先修改下 Makefile 文件, 做法:
在當前 nginx 源碼目錄
$ cd objs $ vi Makefile
# 找到類似這行
&& ./config --prefix=/opt/jediz90/nginx-1.12.2/../openssl-1.0.2g/.openssl no-shared \
# 將 config 修改為 Configure darwin64-x86_64-cc, --prefix 之后的不用修改, 修改后的如:
&& ./Configure darwin64-x86_64-cc --prefix=/opt/jediz90/nginx-1.12.2/../openssl-1.0.2g/.openssl no-shared \
# 修改保存, 反回到上級 nginx 源碼目錄繼續執行 make 即可。