Nginx核心知識100講學習筆記(陶輝):初始Nginx(一)


本文為學習極客時間《nginx核心知識100講》的學習筆記

 https://time.geekbang.org/course/intro/100020301

一、編譯出適合自己的nginx

 1、下載nginx

[root@nginx ~]# wget http://nginx.org/download/nginx-1.14.2.tar.gz

2、介紹各目錄

1、contrib  會有vim語法高亮顏色

[root@nginx nginx-1.14.2]# pwd
/usr/local/src/nginx-1.14.2
[root@nginx nginx-1.14.2]# ls contrib/
geo2nginx.pl  README  unicode2nginx  vim
[root@nginx nginx-1.14.2]# cp -r ./contrib/vim/* /root/.vim

 3、./configure

1、查看幫助文件

[root@nginx nginx-1.14.2]# ./configure --help|more
......
第一類

--prefix=PATH 

第二類:使用哪些和不使用哪些模塊

--with-http_ssl_module          #默認是不編譯進nginx的
--without-http_charset_module   #默認是編譯進nginx,加這個參數就是卸載這個模塊

第三類:特殊優化參數
  --with-cc=PATH                     set C compiler pathname
  --with-cpp=PATH                    set C preprocessor pathname
  --with-cc-opt=OPTIONS              set additional C compiler options
  --with-ld-opt=OPTIONS              set additional linker options
  --with-cpu-opt=CPU  

報錯一:

[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
checking for OS
+ Linux 3.10.0-957.el7.x86_64 x86_64
checking for C compiler ... not found

./configure: error: C compiler cc is not found

解決方法

yum -y install gcc gcc-c++ autoconf automake make

報錯二:

[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx
......
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
[root@nginx nginx-1.14.2]# ./configure --prefix=/usr/local/nginx

解決方法

yum -y install openssl openssl-devel pcre-devel -y

4、中間文件介紹

1、生成中間文件在什么地方?

[root@nginx objs]# pwd
/usr/local/src/nginx-1.14.2/objs
[root@nginx objs]# ll
total 3780
-rw-r--r--. 1 root root 17763 Feb 29 17:44 autoconf.err
-rw-r--r--. 1 root root 39478 Feb 29 17:44 Makefile
-rwxr-xr-x. 1 root root 3746432 Feb 29 17:50 nginx
-rw-r--r--. 1 root root 5341 Feb 29 17:50 nginx.8
-rw-r--r--. 1 root root 6804 Feb 29 17:44 ngx_auto_config.h
-rw-r--r--. 1 root root 657 Feb 29 17:44 ngx_auto_headers.h
-rw-r--r--. 1 root root 5725 Feb 29 17:47 ngx_modules.c        #所有的模塊都放在 ngx_modules.c
-rw-r--r--. 1 root root 31872 Feb 29 17:50 ngx_modules.o
drwxr-xr-x. 9 root root 91 Feb 29 17:44 src

2、為什么要知道nginx編譯中間文件是放在這里呢?

左nginx版本升級,這個時候我們不能make instal 需要把中間文件拷貝到安裝目錄下

3、c語言編輯生成的所有中間目錄都會放在src目錄中

/usr/local/src/nginx-1.14.2/objs/src
[root@nginx src]# ll
total 8
drwxr-xr-x. 2 root root 4096 Feb 29 17:50 core
drwxr-xr-x. 3 root root 168 Feb 29 17:50 event
drwxr-xr-x. 4 root root 4096 Feb 29 17:50 http
drwxr-xr-x. 2 root root 6 Feb 29 17:44 mail
drwxr-xr-x. 2 root root 6 Feb 29 17:44 misc
drwxr-xr-x. 4 root root 31 Feb 29 17:44 os
drwxr-xr-x. 2 root root 6 Feb 29 17:44 stream

如果使用了動態模塊、生成的so文件也會放在這個目錄下

5、編譯安裝

[root@nginx nginx-1.14.2]# make && make install

首次安裝可以使用這個命令 make install

二、Nginx配置語法

1、語法配置

 語法示例

 2、http配置指令塊

 語法示例

 三、nginx的命令行

1、命令行

 四、命令行演示

1、重載配置文件

[root@nginx conf]# ps -ef|grep nginx
root       9597      1  0 15:05 ?        00:00:00 nginx: master process ../sbin/nginx
nobody     9598   9597  0 15:05 ?        00:00:00 nginx: worker process
root       9600   9461  0 15:05 pts/0    00:00:00 grep --color=auto nginx
[root@nginx conf]# vim nginx.conf
開啟:tcp_nopush     on;
[root@nginx conf]# ../sbin/nginx -s reload
[root@nginx conf]# ps -ef|grep nginx
root       9597      1  0 15:05 ?        00:00:00 nginx: master process ../sbin/nginx
nobody     9603   9597  0 15:05 ?        00:00:00 nginx: worker process
root       9605   9461  0 15:05 pts/0    00:00:00 grep --color=auto nginx

2、熱部署

1、nginx運行狀態

[root@nginx sbin]# ps -ef|grep nginx 
root 19530 1 0 18:34 ? 00:00:00 nginx: master process ./nginx
nobody 19531 19530 0 18:34 ? 00:00:00 nginx: worker process
root 19533 9802 0 18:34 pts/0 00:00:00 grep --color=auto nginx

2、備份二進制文件

[root@nginx sbin]# cp nginx nginx.old 
[root@nginx sbin]# cd /usr/local/src/nginx-1.14.2
[root@nginx nginx-1.14.2]# cd objs/
[root@nginx objs]# ll
total 3780
-rw-r--r--. 1 root root 17763 Feb 29 17:44 autoconf.err
-rw-r--r--. 1 root root 39478 Feb 29 17:44 Makefile
-rwxr-xr-x. 1 root root 3746432 Feb 29 17:50 nginx
-rw-r--r--. 1 root root 5341 Feb 29 17:50 nginx.8
-rw-r--r--. 1 root root 6804 Feb 29 17:44 ngx_auto_config.h
-rw-r--r--. 1 root root 657 Feb 29 17:44 ngx_auto_headers.h
-rw-r--r--. 1 root root 5725 Feb 29 17:47 ngx_modules.c
-rw-r--r--. 1 root root 31872 Feb 29 17:50 ngx_modules.o
drwxr-xr-x. 9 root root 91 Feb 29 17:44 src

3、更新nginx二進制文件

[root@nginx objs]# cp -r nginx /usr/local/nginx/ -f #更新nginx二進制文件
[root@nginx objs]# ps -ef|grep nginx
root 19530 1 0 18:34 ? 00:00:00 nginx: master process ./nginx
nobody 19531 19530 0 18:34 ? 00:00:00 nginx: worker process
root 19540 9802 0 18:37 pts/0 00:00:00 grep --color=auto nginx

4、發送一個信號 、master會用新的二進制文件新啟動一個進程

[root@nginx objs]# kill -USR2 19530 
[root@nginx objs]# ps -ef|grep nginx
root 19530 1 0 18:34 ? 00:00:00 nginx: master process ./nginx
nobody 19531 19530 0 18:34 ? 00:00:00 nginx: worker process
root 19541 19530 0 18:38 ? 00:00:00 nginx: master process ./nginx
nobody 19542 19541 0 18:38 ? 00:00:00 nginx: worker process
root 19544 9802 0 18:38 pts/0 00:00:00 grep --color=auto nginx

新的master會生成新的work,老的master和work也在運行,他們會平滑的把所有的請求過度到新的二進制文件啟的進程中

老的master和work已經不再監聽80和443端口 所以新的請求,新的連接會進入新的nginx

5、告訴它請優雅的關閉你的所有的work進程

[root@nginx objs]# kill -WINCH 19530 #
[root@nginx objs]# ps -ef|grep nginx
root 19530 1 0 18:34 ? 00:00:00 nginx: master process ./nginx
root 19541 19530 0 18:38 ? 00:00:00 nginx: master process ./nginx
nobody 19542 19541 0 18:38 ? 00:00:00 nginx: worker process
root 19546 9802 0 18:39 pts/0 00:00:00 grep --color=auto nginx
  1. 老的master進程還在運行、所有的請求已經轉接到新的nginx上了 但是我們又可能會發生一些問題
  2. 新版本退回到老版本,所以我們還可以給老的master發送信號重新把老的work拉起來
  3. 是不會自動退出 允許我們做版本回退

3、切割日志文件

[root@nginx logs]# mv access.log luoahong.log
[root@nginx logs]# ll
total 16
-rw-r--r--. 1 root root 520 Feb 29 18:38 error.log
-rw-r--r--. 1 root root 501 Feb 29 18:35 luoahong.log
-rw-r--r--. 1 root root   6 Feb 29 18:38 nginx.pid
-rw-r--r--. 1 root root   6 Feb 29 18:34 nginx.pid.oldbin
[root@nginx logs]# ../sbin/nginx -s reopen
[root@nginx logs]# ll
total 20
-rw-r--r--. 1 nobody root 292 Feb 29 19:01 access.log
-rw-r--r--. 1 nobody root 581 Feb 29 19:00 error.log
-rw-r--r--. 1 root   root 501 Feb 29 18:35 luoahong.log
-rw-r--r--. 1 root   root   6 Feb 29 18:38 nginx.pid
-rw-r--r--. 1 root   root   6 Feb 29 18:34 nginx.pid.oldbin

五、靜態資源服務演示

1、location

代碼

    server {
        listen       8080;
        server_name  www.luoahong.com;

        #charset koi8-r;

        access_log  logs/access.log  main;

        location / {
            alias    dlib/;
        }

截圖

2、設置目錄瀏覽autoindex on;

實現代碼

    server {
        listen       8080;
        server_name  www.luoahong.com;

        #charset koi8-r;

        access_log  logs/access.log  main;

        location / {
            alias    dlib/;
            autoindex on;
        }

效果截圖

3、控制速度set $limit_rate 1k;

Nginx的http核心模塊ngx_http_core_module中提供limit_rate這個指令可以用於控制速度,limit_rate_after用於設置http請求傳輸多少字節后開始限速。

實現代碼

    server {
        listen       8080;
        server_name  www.luoahong.com;

        #charset koi8-r;

        access_log  logs/access.log  main;

        location / {
            alias    dlib/;
            autoindex on;
			set $limit_rate 1k;

        }

效果截圖

4、壓縮

代碼實現

http {
......

    gzip  on;
    gzip_min_length 1;
    gzip_comp_level 2;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/x=httpd-php image/jpeg image/gif image/png;

    server {
......
        }

效果截圖


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM