1.安裝Nginx編譯所依賴的包html
正常centos中可使用yum安裝一下依賴包:java
yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel
依賴包說明:linux
一、編譯依賴 gcc 環境,因此須要:gcc gcc-c++;nginx
二、PCRE(Perl Compatible Regular Expressions) 是一個Perl庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,因此須要在 linux 上安裝 pcre 庫,pcre-devel 是使用 pcre 開發的一個二次開發庫,因此須要:pcre pcre-devel ;c++
三、zlib 庫提供了不少種壓縮和解壓縮的方式, nginx 使用 zlib 對 http 包的內容進行 gzip ,因此須要在 Centos 上安裝 zlib 庫,因此須要:zlib zlib-devel ;正則表達式
四、OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、經常使用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。nginx 不只支持 http 協議,還支持 https(即在ssl協議上傳輸http),因此須要在 Centos 安裝 OpenSSL 庫,因此須要:openssl openssl-devel ;算法
若是經過yum沒法安裝某個依賴包,能夠下載下來,經過解壓、make && make install的方式安裝centos
我在使用yum安裝pcre時就就沒有安裝成功,致使在make時,報錯:瀏覽器
make: *** No rule to make target `build', needed by `default'. Stop.
若是使用yum安裝不上pcre時,能夠去官網(https://ftp.pcre.org/pub/pcre/)下載對應的壓縮包,再解壓安裝:安全
tar zxvf pcre-8.43.tar.gz cd pcre-8.43 ./configure make && make install
若是使用yum安裝OpenSSL失敗是,能夠去(https://www.openssl.org/source/)下載OpenSSL壓縮包,解壓安裝:
tar zxvf openssl-1.0.2t.tar.gz cd openssl-1.0.2t ./config --prefix=/usr/local/ --openssldir=/usr/local/openssl -g3 shared zlib-dynamic enable-camellia make && make install
測試是否可用:opensslversion
若是zlib使用yum安裝失敗,能夠去http://www.zlib.net/下載壓縮包,解壓安裝:
tar zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make && make install
2.下載安裝Nginx
tar zxvf nginx-1.16.1.tar.gz cd nginx-1.16.1 ./configure --prefix=/opt/nginx/server make && make install
這樣Nginx安裝的sbin,conf相關的目錄就會在/opt/nginx/server中生成,
[root@s1 sbin]# ./nginx -V nginx version: nginx/1.16.1 built by gcc 4.4.6 20120305 (Red Hat 4.4.6-4) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/opt/nginx/server --with-http_stub_status_module --with-http_ssl_module
這裏的 --with-http_stub_status_module --with-http_ssl_module 是配置https時須要添加的ssl模塊,後面會有介紹,若是沒有這兩個模塊使用https時會有報錯:
nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in /opt/nginx/server/conf/nginx.conf:37
cd /opt/nginx/server/sbin
./nginx
./nginx -t #驗證nginx.conf文件正確性 ./nginx -s reload #從新加載nginx.conf文件 ./nginx -s stop #中止Nginx服務
能夠查看端口:
[root@s1 sbin]# netstat -ntlp | grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 349/nginx: master
也能夠經過瀏覽器:http://ip
至此Nginx正常配置已完成,可是若是要是使用https,還須要配置證書和Nginx支持https相關的模塊
首先,Nginx添加支持https的模塊,在安裝時./configuration 須要添加ssl相關模塊:
./configure --prefix=/opt/nginx/server --with-http_stub_status_module --with-http_ssl_module
生成證書:
1.首先使用openssl執行以下命令生成一個key:
openssl genrsa -des3 -out nginx.key 1024
而後他會要求你輸入這個key文件的密碼。不推薦輸入。由於之後要給nginx使用。每次reload nginx配置時候都要你驗證這個PAM密碼的。
因爲生成時候必須輸入密碼。你能夠輸入後 再刪掉。
mv nginx.key xxx.key openssl rsa -in xxx.key -out nginx.key rm xxx.key
2.而後使用openssl 根據這個key文件生成證書請求文件:
openssl req -new -key nginx.key -out nginx.csr
以上命令生成時候要填不少東西 一個個看着寫吧(能夠隨便,畢竟這是本身生成的證書,可是若是使用java程序訪問時,須要將在輸入用戶名或服務器名時,輸入本身的域名,否則會報找不到匹配的域名證書錯誤)
3.最後根據這2個文件生成crt證書文件:
openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.crt
4.最後使用到的文件是key和crt文件。若是須要用pfx 能夠用如下命令生成:
openssl pkcs12 -export -inkey nginx.key -in nginx.crt -out nginx.pfx
配置nginx https:
須要在nginx.conf配置文件中添加:
server { listen 443 ssl; server_name httpfs.test.com; ssl_protocols SSLv2 SSLv3 TLSv1; #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_certificate /opt/nginx/ssl/nginx.crt; ssl_certificate_key /opt/nginx/ssl/nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_prefer_server_ciphers on; location / { proxy_pass http://httpfs/; } }
個人nginx.conf配置文件:
#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; upstream httpfs { server 127.0.0.1:14000; } server { listen 80; server_name httpfs.test.com; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://httpfs/; } #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; } } # HTTPS server server { listen 443 ssl; server_name httpfs.test.com; ssl_protocols SSLv2 SSLv3 TLSv1; #ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; ssl_certificate /opt/nginx/ssl/nginx.crt; ssl_certificate_key /opt/nginx/ssl/nginx.key; ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; #ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { proxy_pass http://httpfs/; } } }
重啓nginx後,使用https訪問就能夠了。
【注意】
1.若是不能訪問,請檢查防火牆配置
2.若是不能訪問,能夠將域名配置到hosts中