nginx和ssl的安裝使用
最近想把網站加上ssl證書,只能在nginx上使用,就順便了解下nginx,我用的是centos6.5,開始
1.下載nginx及相關組件
切換到root用戶,進入存放下載文件的目錄 cd /usr/local/src 我是準備下載在這的,可以自己更改
開始下載
[root@localhost src]# wget http://nginx.org/download/nginx-1.10.2.tar.gz [root@localhost src]# wget http://www.openssl.org/source/openssl-fips-2.0.10.tar.gz [root@localhost src]# wget http://zlib.net/zlib-1.2.11.tar.gz [root@localhost src]# wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-8.40.tar.gz
2.安裝nginx及相關組件
nginx安裝
[root@localhost src]# tar zxvf nginx-1.10.2.tar.gz [root@localhost src]# cd nginx-1.10.2 [root@localhost nginx-1.10.2]# ./configure && make && make install
zlib安裝
[root@localhost src]# tar zxvf zlib-1.2.11.tar.gz [root@localhost src]# cd zlib-1.2.11 [root@localhost zlib-1.2.11]# ./configure && make && make install
pcre安裝
[root@localhost src]# tar zxvf pcre-8.40.tar.gz [root@localhost src]# cd pcre-8.40 [root@localhost pcre-8.40]# ./configure && make && make install
openssl安裝
[root@localhost src]# tar zxvf openssl-fips-2.0.10.tar.gz [root@localhost src]# cd openssl-fips-2.0.10 [root@localhost openssl-fips-2.0.10]# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module
說明: ./configure --prefix=nginx安裝路徑 --with-模塊
查看nginx安裝路徑 whereis nginx
3.對nginx進行配置
nginx的基本操作
啟動
[root@localhost ~]# /usr/local/nginx/sbin/nginx
停止/重啟
[root@localhost ~]# /usr/local/nginx/sbin/nginx -s stop(quit、reload)
命令幫助
[root@localhost ~]# /usr/local/nginx/sbin/nginx -h
驗證配置文件
[root@localhost ~]# /usr/local/nginx/sbin/nginx -t
配置文件
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
vi打開文件後的基本操作
默認vi打開後是不能錄入的,需要按鍵才能操作,具體如下:
開啟編輯:按“i”或者“Insert”鍵
退出編輯:“Esc”鍵
退出vi:“:q”
保存vi:“:w”
保存退出vi:“:wq”
不保存退出vi:“:q!”
打開nginx的配置文件
vi /usr/local/nginx/conf/nginx.conf
以下是我的配置
server { listen 80; server_name www.hushunwei.com; rewrite ^(.*) https://$host$1 permanent; #http自動跳轉到https #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://www.hushunwei.com:9000; //項目原訪問路徑 root /usr/java/tale/resources/templates/themes/default/; #頁面路徑 index index.html index.htm; client_max_body_size 1000m; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ #過濾圖片 { proxy_pass https://www.hushunwei.com; } location ~ .*\.(js|css)?$ #過濾js和css,避免被攔截 { proxy_pass https://www.hushunwei.com; }
以上就能實現訪問9000端口的時候通過nginx轉發到80端口,接下來就是我要做的配置ssl
我是購買的阿裏雲的域名,所以在阿裏雲申請ssl證書,
配置ssl,以下是我的配置
server { listen 443; server_name www.hushunwei.com; ssl on; ssl_certificate cert/1526657460145.pem; ssl_certificate_key cert/1526657460145.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; location / { root /usr/java/tale/resources/templates/themes/default/; index index.html index.htm; client_max_body_size 1000m; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://www.hushunwei.com:9000; } }
4.啟動nginx
[root@localhost ~]# /usr/local/nginx/sbin/nginx
## 5.訪問網址
輸入網址後自動跳轉到https鏈接
nginx和ssl的安裝使用