Nginx在Linux下的安裝
重新準備一臺虛擬機作爲服務器。比如IP地址爲192.168.222.132
環境準備
(1)需要安裝 gcc 的環境:
yum install gcc-c++
(2)第三方的開發包
PCRE
PCRE(Perl Compatible Regular Expressions)是一個 Perl 庫,包括 perl 兼容的正則表達式庫。nginx 的 http 模塊使用 pcre 來解析正則表達式,所以需要在 linux 上安裝 pcre 庫。
yum install -y pcre pcre-devel
注:pcre-devel 是使用 pcre 開發的一個二次開發庫。nginx 也需要此庫。
zlib
zlib 庫提供了很多種壓縮和解壓縮的方式,nginx 使用 zlib 對 http 包的內容進行 gzip,所以需要在 linux 上安裝 zlib 庫。
yum install -y zlib zlib-devel
OpenSSL
OpenSSL 是一個強大的安全套接字層密碼庫,囊括主要的密碼算法、常用的密鑰和證書封裝管理功能及 SSL 協議,並提供豐富的應用程序供測試或其它目的使用。nginx 不僅支持 http 協議,還支持 https(即在 ssl 協議上傳輸 http),所以需要在 linux安裝 openssl 庫。
yum install -y openssl openssl-devel
Nginx下載
官方網站下載 nginx:http://nginx.org/
我使用的版本是 1.8.0 版本
Nginx安裝
第一步:把 nginx 的源碼包nginx-1.8.0.tar.gz上傳到 linux 系統
Alt+p 啓動sftp ,將d盤setup文件夾中的nginx-1.8.0.tar.gz上傳
輸入命令:put d:/setup/nginx-1.8.0.tar.gz
第二步:解壓縮
tar zxvf nginx-1.8.0.tar.gz
第三步:進入nginx-1.8.0目錄 使用 configure 命令創建一 makeFile 文件。
./configure \
--prefix=/usr/local/nginx \
--pid-path=/var/run/nginx/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--with-http_stub_status_module \
--with-http_ssl_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
\ 代表換行
執行後可以看到Makefile文件
第四步:編譯
make
第五步:安裝
make install
Nginx啓動與訪問
注意:啓動nginx 之前,上邊將臨時文件目錄指定爲/var/temp/nginx/client, 需要在/var 下創建此 目錄
mkdir /var/temp/nginx/client -p
進入到Nginx目錄下的sbin目錄
cd /usr/local/ngiux/sbin
輸入命令啓動Nginx
./nginx
啓動後查看進程
ps aux|grep nginx
地址欄輸入虛擬機的IP即可訪問(默認爲80端口)
配置https
1、生成證書步驟
cd /usr/local/nginx/conf
a、//生成私鑰
openssl genrsa -des3 -out server.key 1024
密碼輸入:123456
繼續輸入123456
b、//生成證書請求文件
openssl req -new -key server.key -out server.csr
c、//生成證書(測試時可使用)
openssl req -x509 -days 3650 -key server.key -in server.csr > server.crt
server.key輸入123456
說明:36500天表示一百年,這是用步驟a,b的的密鑰和證書請求生成證書server.crt,-days參數指明證書有效期,單位爲天,x509表示生成的爲X.509證書。以上籤署證書僅僅做測試用,真正運行的時候,應該將CSR發送到一個CA返回真正的證書。網上有些文檔描述生成證書文件的過程比較繁瑣,就是因爲 他們自己建立了一個CA中心,然後再簽署server.csr。
用openssl x509 -noout -text -in server.crt 可以查看證書的內容。證書實際上包含了Public Key
d、//生成無密的私鑰(可避免nginx配置SSL安全證書後,重啓免輸入密碼)
openssl rsa -in server.key -out server.key.unsecure
server.key輸入123456
e、將crt證書格式導出爲cer證書格式,供ios客服端調用使用
openssl x509 -in server.crt -out server.cer -outform der
nginx的配置:
這裏使用EditPlus編輯nginx的配置文件。關於EditPlus怎麼連接linux,不知道的小夥伴可以看下這個鏈接:https://blog.csdn.net/eagleuniversityeye/article/details/79998348
重啓nginx
在瀏覽器中輸入https://192.168.222.132
點擊高級
https配置成功
一些工具使用:
1、查看證書日期
openssl x509 -in server.crt -noout -dates
以上說明證書有效期爲100年
現在網站是升級https了,但是用戶又不知道,而且不輸入協議頭的話瀏覽器默認都是按照http來加載,所以我們還要對http做301 自動跳轉處理。
方式一:
在配置文件中增加一個虛擬主機:
重啓nginx:
./nginx -s reload
在瀏覽器中輸入192.168.222.132/
上述自動跳轉配置會自動攜帶URL和參數,例如,訪問 http://192.168.222.132/?a=1 會自動跳轉到 http://192.168.222.132/?a=1。
方式二:
利用meta的刷新作用將http跳轉到https-
上述的方法均會耗費服務器的資源,可以借鑑百度使用的方法:巧妙的利用meta的刷新作用,將http跳轉到https,可以基於http://dev
.wangshibo.com的虛擬主機路徑下寫一個index.html,內容就是http向https的跳轉
將下面的內容追加到index.html首頁文件內
新建文件夾:/var/www/html/8080/
mkdir /var/www/html/8080/ -p
新建文件index.html
touch index.html
用EditPlus打開index.html
將以下代碼拷貝到index.html中
<html>
<meta http-equiv="refresh" content="0;url=https://192.168.222.132/">
</html>
修改配置文件:
重啓nginx
在瀏覽器中輸入192.168.222.132。將會自動跳轉到https://192.168.222.132
以上https的證書是我自己生成的,所有瀏覽器仍然會顯示不安全字樣。需要到正規渠道購買證書才行。
這裏貼一下配置文件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;
server {
listen 443 ssl;
server_name localhost;
charset utf-8;
ssl on;
ssl_certificate /usr/local/nginx/conf/server.crt;
ssl_certificate_key /usr/local/nginx/conf/server.key.unsecure;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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;
#}
}
server {
listen 80;
server_name 192.168.222.132;
index index.html index.php index.htm;
access_log /usr/local/nginx/logs/8080-access.log;
error_log /usr/local/nginx/logs/8080-error.log;
#將404的頁面重定向到https的首頁
error_page 404 https://192.168.222.132/;
location ~ / {
root /var/www/html/8080;
index index.html index.php index.htm;
}
}
# 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;
# }
#}
}