Nginx編譯安裝(以及JSON格式日志、自定義錯誤頁和https配置)

目錄:

一:Nginx編譯安裝
1.1:準備編譯安裝的基礎環(huán)境
1.2:下載Nginx源碼包
1.3:編譯安裝Nginx
1.4:創(chuàng)建nginx用戶
1.5:創(chuàng)建unitfile,以使用systemd進行管理
1.6:創(chuàng)建nginx命令軟鏈
二:自定義404錯誤頁面
2.1:配置站點
2.2:自定義404錯誤頁面
三:配置json格式的訪問日志
四:配置https站點
4.1:生成自簽名CA證書
4.2:生成私鑰key和證書申請csr
4.3:充當CA機構給自己簽發(fā)證書
4.4:Nginx的https配置4.5:通過https訪問站點

一:Nginx編譯安裝

1.1:準備編譯安裝的基礎環(huán)境

編譯安裝Nginx前,需要安裝一些基礎程序包:

  • gcc:C語言編譯器,因為Nginx是由C語言開發(fā)的;
  • automake:從Makefile.am文件自動生成Makefile.in的工具;
  • pcre、pcre-devel:提供正則表達式語法支持,因為Nginx的rewrite模塊和HTTP核心模塊會使用正則表達式實現(xiàn)一些匹配功能;
  • zlib-devel:nginx啟用壓縮功能時,需要此模塊的支持;
  • openssl、openssl-devel:開啟SSL以實現(xiàn)https時,需要此模塊的支持;
  • ……

執(zhí)行下方命令準備編譯基礎環(huán)境:

yum install -y vim lrzsz tree screen psmisc lsof tcpdump wget ntpdate gcc gcc-c++ glibc glibc-devel pcre pcre-devel openssl openssl-devel systemd-devel net-tools iotop bc zip unzip zlib-devel bash-completion nfs-utils automake libxml2 libxml2-devel libxslt libxslt-devel perl perl-ExtUtils-Embed

1.2:下載Nginx源碼包

官方下載地址:https://nginx.org/en/download.html

Nginx官網(wǎng)提供了三個類型的版本:

  • Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以說是開發(fā)版
  • Stable version:最新穩(wěn)定版,生產(chǎn)環(huán)境上建議使用的版本
  • Legacy versions:遺留的老版本的穩(wěn)定版
1.jpg

這里下載nginx-1.18.0這個穩(wěn)定版本,下載完成后傳至要安裝的Linux主機,準備進行編譯安裝。
也可以右鍵復制鏈接地址,在Linux主機上直接wget進行下載。

1.3:編譯安裝Nginx

1.準備源碼包,按慣例會把源碼包放在/usr/local/src下:

~]# cd /usr/local/src
src]# wget https://nginx.org/download/nginx-1.18.0.tar.gz
src]# tar zxf nginx-1.18.0.tar.gz
src]# ll
drwxr-xr-x 8 1001 1001     147 Apr 21  2020 nginx-1.18.0

2.執(zhí)行configure生成Makefile

src]# cd nginx-1.18.0/
nginx-1.18.0]# ./configure --prefix=/apps/nginx-1.18.0 \
--user=nginx \
--user=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

#編譯參數(shù)意為:
#--prefix=/apps/nginx-1.18.0:安裝目錄為/apps/nginx-1.18.0,為區(qū)分各版本,最好加上版本號;
#--user=nginx、--user=nginx:指定nginx進程的啟動用戶及用戶組為nginx、nginx;
#--with-……:指定要啟用的Nginx模塊,這里啟用的有ssl(支持https)、stub_status(提供nginx狀態(tài)頁)、pcre(正則表達式)、stream(四層負載均衡)等模塊。

#./configure后,Makefile文件中會生成如下內容,make會根據(jù)Makefile的內容進行編譯
nginx-1.18.0]# cat Makefile 
default:        build
clean:
        rm -rf Makefile objs
build:
        $(MAKE) -f objs/Makefile
install:
        $(MAKE) -f objs/Makefile install
modules:
        $(MAKE) -f objs/Makefile modules
upgrade:
        /apps/nginx-1.18.0/sbin/nginx -t
        kill -USR2 `cat /apps/nginx-1.18.0/logs/nginx.pid`
        sleep 1
        test -f /apps/nginx-1.18.0/logs/nginx.pid.oldbin
        kill -QUIT `cat /apps/nginx-1.18.0/logs/nginx.pid.oldbin`

3.執(zhí)行make進行編譯

nginx-1.18.0]# make

4.執(zhí)行make install進行安裝

nginx-1.18.0]# make install

#驗證Nginx版本及編譯參數(shù):
nginx-1.18.0]# /apps/nginx-1.18.0/sbin/nginx -V
nginx version: nginx/1.18.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
built with OpenSSL 1.0.2k-fips  26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/apps/nginx-1.18.0 --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

#通過-V參數(shù)可以看到nginx版本以及安裝過程中的編譯參數(shù),這些編譯參數(shù)可以在日后更改編譯參數(shù)時使用。

1.4:創(chuàng)建nginx用戶

useradd nginx -s /sbin/nologin -u 2000
chown nginx.nginx -R /apps/nginx-1.18.0/
#創(chuàng)建nginx用戶最好指定UID,以便管理,并安全起見,使用nologin

1.5:創(chuàng)建unitfile,以使用systemd進行管理

一般需要在unitfile中修改的參數(shù)有:

  1. PIDFile:指定PID文件,PIDFile必須和nginx.conf中的pid保持一致;
  2. ExecStartPre:啟動nginx前需要執(zhí)行的命令;
  3. ExecStart:啟動命令。
#unitfile文件名取nginx-1180.service,以區(qū)分版本
~]# vim /usr/lib/systemd/system/nginx-1180.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/apps/nginx-1.18.0/logs/nginx.pid
# Nginx will fail to start if /run/nginx.pid already exists but has the wrong
# SELinux context. This might happen when running `nginx -t` from the cmdline.
# https://bugzilla.redhat.com/show_bug.cgi?id=1268621
ExecStartPre=/usr/bin/rm -f /apps/nginx-1.18.0/logs/nginx.pid 
ExecStartPre=/apps/nginx-1.18.0/sbin/nginx -t
ExecStart=/apps/nginx-1.18.0/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true

[Install]
WantedBy=multi-user.target

#核對或更改nginx.conf中的pid
~]# vim /apps/nginx-1.18.0/conf/nginx.conf
#pid        logs/nginx.pid;

# 驗證unitfile
~]# systemctl daemon-reload
~]# systemctl start nginx-1180
~]# systemctl status nginx-1180
~]# systemctl stop nginx-1180

1.6:創(chuàng)建nginx命令軟鏈

~]# ln -sv /apps/nginx-1.18.0/sbin/nginx /usr/sbin/nginx-1180

#測試命令軟鏈
~]# nginx-1180 -t
nginx: the configuration file /apps/nginx-1.18.0/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx-1.18.0/conf/nginx.conf test is successful

至此,nginx編譯安裝完成。

二:自定義404錯誤頁面

2.1:配置站點

利用Nginx配置文件模塊化的功能,配置一個域名為www.yqc.com的站點:

#創(chuàng)建模塊化配置文件目錄:
~]# cd /apps/nginx-1.18.0/
nginx-1.18.0]# mkdir conf.d

#配置使nginx識別模塊化配置文件,并更改默認站點的端口為8080,以與后邊的測試不沖突:
nginx-1.18.0]# vim conf/nginx.conf
……
http {
    ……
    include /apps/nginx-1.18.0/conf.d/*.conf;
    ……
    server {
        listen       8080;
        ……
}

#配置站點:
nginx-1.18.0]# vim conf.d/yqc.conf
server {
        listen 80;
        server_name www.yqc.com;
        location / {
                root /data/nginx/html;
                index index.html index.htm;
        }
}

#創(chuàng)建測試頁面
~]# mkdir /data/nginx/html/
~]# vim /data/nginx/html/index.html
yqc page

#啟動nginx并訪問:
nginx-1.18.0]# nginx-1180 -t
nginx-1.18.0]# systemctl start nginx-1180

#客戶端使用curl命令進行訪問:
~]# vim /etc/hosts
192.168.43.219  www.yqc.com
~]# curl www.yqc.com
yqc page

2.2:自定義404錯誤頁面

nginx-1.18.0]# vim conf.d/yqc.conf 
server {
        listen 80;
        server_name www.yqc.com;
        error_page 404 /404.html;
        location / {
                root /data/nginx/html;
                index index.html index.htm;
        }
}

#檢查配置并重置
nginx-1.18.0]# nginx-1180 -t
nginx-1.18.0]# nginx-1180 -s reload

#創(chuàng)建404錯誤頁面
nginx-1.18.0]# vim /data/nginx/html/404.html
404 error

#因為404表示訪問的頁面不存在,所以在客戶端隨便訪問一個不存在的頁面進行測試:
~]# curl www.yqc.com/hehe.html
404 error

三:配置json格式的訪問日志

一般在主配置文件的http模塊定義日志格式,這樣各站點的配置文件就都可以調用了:

#主配置文件定義json日志格式
#注意日志格式要配置在include之上,否則因為順序檢查的原因,在檢查到conf.d/yqc.conf時,會報沒有access_json這個日志格式的錯誤
nginx-1.18.0]# vim conf/nginx.conf
……
http {
    ……
    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"}';
    include /apps/nginx-1.18.0/conf.d/*.conf;
    ……
}

#站點配置文件進行調用:
nginx-1.18.0]# vim conf.d/yqc.conf 
server {
        listen 80;
        server_name www.yqc.com;
        error_page 404 /404.html;
        access_log /apps/ngins-1.18.0/logs/access_json.log;
        location / {
                root /data/nginx/html;
                index index.html index.htm;
        }
}

#檢查配置并重置
nginx-1.18.0]# nginx-1180 -t
nginx-1.18.0]# nginx-1180 -s reload

#打開json格式的日志文件,并用客戶端訪問,查看效果:
nginx-1.18.0]# tail -f logs/access_json.log
{"@timestamp":"2020-06-08T18:43:11+08:00","host":"192.168.43.219","clientip":"192.168.43.102","size":9,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.yqc.com","uri":"/index.html","domain":"www.yqc.com","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.29.0","status":"200"}
{"@timestamp":"2020-06-08T18:43:12+08:00","host":"192.168.43.219","clientip":"192.168.43.102","size":9,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.yqc.com","uri":"/index.html","domain":"www.yqc.com","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.29.0","status":"200"}
{"@timestamp":"2020-06-08T18:43:12+08:00","host":"192.168.43.219","clientip":"192.168.43.102","size":9,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.yqc.com","uri":"/index.html","domain":"www.yqc.com","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.29.0","status":"200"}
{"@timestamp":"2020-06-08T18:43:13+08:00","host":"192.168.43.219","clientip":"192.168.43.102","size":10,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.yqc.com","uri":"/404.html","domain":"www.yqc.com","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.29.0","status":"404"}
{"@timestamp":"2020-06-08T18:43:15+08:00","host":"192.168.43.219","clientip":"192.168.43.102","size":10,"responsetime":0.000,"upstreamtime":"-","upstreamhost":"-","http_host":"www.yqc.com","uri":"/404.html","domain":"www.yqc.com","xff":"-","referer":"-","tcp_xff":"-","http_user_agent":"curl/7.29.0","status":"404"}

#已經(jīng)可以獲取到json格式的訪問日志了。

四:配置https站點

實際應用中,需要將證書申請文件csr提交給專門的CA機構,CA機構根據(jù)自己的私鑰和CA證書來制作相應的證書crt;
這里是自己生成自簽名的CA證書,充當CA機構來完成自己的證書制作。

4.1:生成自簽名CA證書

~]# mkdir /apps/nginx-1.18.0/certs
~]# cd /apps/nginx-1.18.0/certs

#生成自簽名的CA證書(這里指CA機構自己的證書)
certs]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout ca.key -x509 -days 3650 -out ca.crt

#查看已生成的CA證書和私鑰
certs]# ll
total 8
-rw-r--r--. 1 root root 2049 Jun  8 22:16 ca.crt
-rw-r--r--. 1 root root 3272 Jun  8 22:16 ca.key

4.2:生成私鑰key和證書申請csr

certs]# openssl req -newkey rsa:4096 -nodes -sha256 -keyout www.yqc.com.key -out www.yqc.com.csr
# 注意“A challenge password []:” 這一步不要輸入密碼,不然配置了ssl后,nginx每一次重載配置都要輸入該密碼。

4.3:充當CA機構給自己簽發(fā)證書

實際中,相當于把csr交給CA機構,由他們來簽發(fā)證書。

certs]# openssl x509 -req -days 3650 -in www.yqc.com.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out www.yqc.com.crt

#可以使用下列命令來查看證書中的信息,驗證證書:
certs]# openssl x509 -in www.yqc.com.crt -noout -text
Certificate:
    Data:
        Version: 1 (0x0)
        Serial Number:
            b8:36:13:cf:c2:68:a5:9e
    Signature Algorithm: sha256WithRSAEncryption
        Issuer: C=CN, ST=SX, L=TY, O=YQC, OU=YQC, CN=yqc.ca/emailAddress=20251839@qq.com
        Validity
            Not Before: Jun  8 14:26:13 2020 GMT
            Not After : Jun  6 14:26:13 2030 GMT
        Subject: C=CN, ST=SX, L=TY, O=YQC, OU=YQC, CN=www.yqc.com/emailAddress=20251839@qq.com
        Subject Public Key Info:
            Public Key Algorithm: rsaEncryption
                Public-Key: (4096 bit)
                Modulus:
                ……

4.4:Nginx的https配置

實際中,相當于CA機構將簽發(fā)好的證書交給我們,就是www.yqc.com.crt這個文件;
然后結合我們的私鑰www.yqc.com.key,就可以實現(xiàn)https功能了。

#在www.yqc.com站點上配置https:
#指定監(jiān)聽端口為443,并聲明使用ssl,指定證書和私鑰,就可以實現(xiàn)站點的https訪問了。
#額外再為https配置會話緩存,緩存名稱為sslcache,大小為20m;https會話超時時間為10m。
nginx-1.18.0]# vim conf.d/yqc.conf 
server {
        listen 80;
        listen 443 ssl;
        ssl_certificate /apps/nginx-1.18.0/certs/www.yqc.com.crt;
        ssl_certificate_key /apps/nginx-1.18.0/certs/www.yqc.com.key;
        ssl_session_cache shared:sslcache:20m;
        ssl_session_timeout 10m;
        server_name www.yqc.com;
        error_page 404 /404.html;
        access_log /apps/nginx-1.18.0/logs/access_json.log access_json;
        location / {
                root /data/nginx/html;
                index index.html index.htm;
        }
}

#檢查配置并重載
nginx-1.18.0]# nginx-1180 -t
nginx-1.18.0]# nginx-1180 -s reload

4.5:通過https訪問站點

#在windows的HOSTS文件中添加一條解析記錄
C:\Windows\System32\drivers\etc\HOSTS
    192.168.43.219 www.yqc.com

客戶端瀏覽器訪問https://www.yqc.com,驗證配置

1.jpg

點擊繼續(xù)前往,可以訪問到www.yqc.com的主頁:

1.jpg
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內容