准備源碼包,並解壓,創建nginx用戶
[root@slave-master ~]# tar xf nginx-1.16.0.tar.gz [root@slave-master ~]# useradd -r -s /sbin/nologin nginx [root@slave-master ~]# cd nginx-1.16.0
准備開發包組
[root@slave-master nginx-1.16.0]# yum install gcc pcre-devel openssl-devel zlib-devel -y
開始編譯安裝
[root@slave-master nginx-1.16.0]# ./configure --prefix=/apps/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_v2_module --with-http_realip_module --with-http_stub_status_module --with-http_gzip_static_module --with-pcre --with-stream --with-stream_ssl_module --with-stream_realip_module [root@slave-master nginx-1.16.0]# make -j 4 && make install
修改路徑並啟動
[root@slave-master nginx-1.16.0]# cd /apps/nginx/ [root@slave-master sbin]# ln -sv /apps/nginx/sbin/* /usr/sbin
[root@slave-master sbin]# nginx
基於basic認證,先修改主配置文件,讓它包含一個目錄,我們就可以單獨寫配置文件
[root@slave-master src]# vim /apps/nginx/conf/nginx.conf http { include mime.types; default_type application/octet-stream; include /apps/nginx/conf/src/*.conf; [root@slave-master src]# mkdir /apps/nginx/conf/src/
[root@slave-master src]# vim /apps/nginx/conf/src/nginx.conf
server {
server_name www.magedu.com;
location / {
root /apps/nginx/conf/src/html;
auth_basic "renzheng";
auth_basic_user_file "/apps/nginx/conf/src/.nginx";
}
}
利用htpasswd命令創建用戶
[root@slave-master src]# htpasswd -c /apps/nginx/conf/src/.nginx bob
New password:
Re-type new password:
測試
實現status頁面
server { server_name www.magedu.com; access_log /apps/nginx/conf/src/access.log main; location / { root /apps/nginx/conf/src/html; auth_basic "renzheng"; auth_basic_user_file "/apps/nginx/conf/src/.nginx"; } location = /nginx_status { stub_status; allow 127.0.0.1; deny all; } }
基於JSON格式的訪問日志
[root@slave-master src]# vim nginx.conf log_format access_json '{"@timestamp":"$time_iso8601",' '"host":"$server_addr",' '"clientip":"$remote_addr",' '"size":$body_bytes_sent,' '"responsetime":$request_time,' '"upstreamtime":"$upstream_response_time",' '"upstreamhost":"$upstream_addr",' '"http_host":"$host",' '"uri":"$uri",' '"domain":"$host",' '"xff":"$http_x_forwarded_for",' '"referer":"$http_referer",' '"tcp_xff":"$proxy_protocol_addr",' '"http_user_agent":"$http_user_agent",' '"status":"$status"}'; server { server_name www.magedu.com; access_log /apps/nginx/conf/src/access.log main; location / { root /apps/nginx/conf/src/html; auth_basic "renzheng"; auth_basic_user_file "/apps/nginx/conf/src/.nginx"; access_log /apps/nginx/conf/src/access_json.log access_json;
啟用壓縮功能
[root@slave-master messge]# vim ../nginx.conf location /messge { root /apps/nginx/conf/src; gzip on; gzip_comp_level 6; gzip_min_length 64; gzip_vary on; gzip_types text/xml text/css application/javascript; }
如果客戶請求的頁面不存在將自動跳轉至首頁
[root@slave-master messge]# vim ../nginx.conf if (!-f $request_filename) { rewrite (.*) http://www.magedu.com/index.html; }
ssl
[root@slave-master ssl]# vim ../nginx.conf server { listen 443 ssl; server_name wwww.magedu.com; ssl_certificate "/apps/nginx/conf/src/ssl/magedu.com.crt"; ssl_certificate_key "/apps/nginx/conf/src/ssl/magedu.com.key"; root "/apps/nginx/conf/src/jiami"; }
將http請求跳轉至https
[root@slave-master ssl]# vim ../nginx.conf if ($scheme = http) { rewrite / https://www.magedu.com/ redirect; }
防盜鏈
[root@slave-master ssl]# vim ../nginx.conf valid_referers none blocked server_names *.magedu.com magedu.* www.magedu.org/galleries/ ~\.google\.; if ($invalid_referer) { return 403 "Forbidden Access"; }
配置文件下載
[root@slave-master src]# vim nginx.conf location /download { root /apps/nginx/conf/src; autoindex on; autoindex_exact_size off; autoindex_localtime on; limit_rate 100k; } autoindex on | off; 自動文件索引功能,默為off autoindex_exact_size on | off; 計算文件確切大小(單位bytes),off 顯示大概大小(單位K、M),默認on autoindex_localtime on | off ; 顯示本機時間而非GMT(格林威治)時間,默認off autoindex_format html | xml | json | jsonp; 顯示索引的頁面文件風格,默認html
