Nginx 适用于哪些场景

  1. 静态资源服务
    1. 通过本地文件系统提供服务
  2. 反向代理服务
    1. Nginx的强大性能
    2. 缓存
    3. 负载均衡
  3. API服务
    1. OpenResty
    2. nginx 直接访问数据库,redis

image.png

Nginx 为什么会出现

  1. 互联网的数据量快速增长
  2. 摩尔定律:新能提升
  3. 低效的Apache (一个进程对应一个链接 => 进程间切换 => 性能消耗)

Nginx 的优点

  1. 高并发,高性能

    32C 64G 服务器 轻松达到数千万的并发链接,若简单的静态资源请求 100万rps

  2. 可扩展性好

    模块化设计很好,生态丰富

  3. 高可靠性

    可以持续不间断的运行数年

  4. 热部署

    不停止服务的情况下升级Nginx, 因为 killed Nginx 意味着操作系统对所有已经建立链接的客户端发送一个 tcp reset 复位包,而很多客户端时没有办法很好的处理复位请求的

  5. BSD许可证

    开源

Nginx 的组成

image.png

编译Nginx

官方模块并不是每个默认都开启的,如果想添加第三方Nginx模块,需要自己编译
image.png

  1. wget http://nginx.org/download/nginx-1.20.1.tar.gz
  2. tar -xvf nginx-1.20.1.tar.gz
  1. auto -- 辅助configure脚本执行时,判定nginx执行哪些模块,当前的操作有什么特性可供nginx使用
  2. CHANGES -- 特性和bugfix描述文档
  3. CHANGES.ru -- 俄语版
  4. conf -- 配置示例文件,会将其拷贝到安装目录
  5. configure -- 生成中间文件,执行编译前的必备动作
  6. contrib -- vim 高亮配置, pl 脚本
  7. html -- 实例 html
  8. LICENSE -- LICENSE
  9. man -- 帮助文档
  10. src -- 源码
  1. # 查看 configure 支持哪些参数~
  2. ./configure --help
  3. # --with 表示模块默认不会编译进 nginx, 需要手动加入
  4. # --without 表示模块默认不会编译进 nginx,需要手动移除
  5. ./configure --prefix=/home/geek/nginx
  6. # E: Couldn't find any package by regex 'gcc-c+'
  7. sudo apt-get install build-essential
  8. # ./configure: error: the HTTP rewrite module requires the PCRE library.
  9. sudo apt-get install libpcre3 libpcre3-dev
  10. # ./configure: error: the HTTP gzip module requires the zlib library.
  11. sudo apt-get install zlib1g-dev
  12. # congfigure 执行完后会生成中间文件 objs, ngx_modules.c
  13. make
  14. # 安装
  15. make install
  16. cd pbjs/
  17. cat ngx_modules.c

08丨 编译出适合自己的 Nginx.mp4

Nginx 配置语法

nginx 每个配置模块都有自己的配置语法,遵循同样的语法规则
image.png

配置参数:时间单位

image.png

配置参数:空间的单位

image.png

http 配置的指令块

image.png

  • http 块中的配置,都由http模块去解析
  • upstream 表示上游服务,当nginx 需要与tomcat, django,等其他服务交互时定义
  • server 对应一个域名/一组域名
  • location url 表达式

Nginx 命令行及演示:重载,热备份,日志切割

image.png

  • nginx 操作运行时进程的方法是通过发送信号 -s

重载(配置文件)

  1. nginx -s reload

热部署 (nginx 正在运行,更换最新版本的nginx)

  1. # 查看正在运行的nginx
  2. ps -ef | grep nginx
  3. root 1168 8 0 21:53 ? 00:00:00 nginx: master process nginx
  4. www-data 1169 1168 0 21:53 ? 00:00:00 nginx: worker process
  5. www-data 1170 1168 0 21:53 ? 00:00:00 nginx: worker process
  6. zhanyeye 1182 9 0 21:53 pts/0 00:00:00 grep --color=auto nginx
  7. # 先备份现有nginx二进制文件
  8. cp nginx nginx.old
  9. # cp 替换正在运行的二进制文件
  10. cp -r nginx /usr/local/.../nginx/sbin/ -f
  11. # 向nginx的master进程发送信号:USR2
  12. kill -USR2 1168
  13. # 新老nginx进程 平滑过渡n'gin'x
  14. # 告诉老进程
  15. kill -WINCH 1168

切割日志文件

  1. cd /var/log/nginx
  2. mv access.log backup.log
  3. nginx -s reopen

我们可以卸载bash脚本,做一个定时任务
rotate.sh (ubuntu 20.04)

  1. #!/bin/bash
  2. # Rotate the Nginx logs to prevent a single logfile from consuming too much disk space.
  3. OLD_LOGS_PATH=/var/log/nginx/history
  4. CUR_LOGS_PATH=/var/log/nginx
  5. YESTERDAY=${date -d "yesterday" +%Y-%m-%d}
  6. mv ${CUR_LOGS_PATH}/access.log ${OLD_LOGS_PATH}/access.log
  7. mv ${CUR_LOGS_PATH}/error.log ${OLD_LOGS_PATH}/error.log
  8. # 向 nginx 主进程发送 usr1 信号, usr1 信号是重新打开日志文件
  9. kill -USR1 $(cat /run/nginx.pid)

nginx conf

  1. user www-data;
  2. worker_processes auto;
  3. pid /run/nginx.pid;
  4. include /etc/nginx/modules-enabled/*.conf;
  5. events {
  6. worker_connections 768;
  7. # multi_accept on;
  8. }
  9. http {
  10. ##
  11. # Basic Settings
  12. ##
  13. sendfile on;
  14. tcp_nopush on;
  15. tcp_nodelay on;
  16. keepalive_timeout 65;
  17. types_hash_max_size 2048;
  18. # server_tokens off;
  19. # server_names_hash_bucket_size 64;
  20. # server_name_in_redirect off;
  21. include /etc/nginx/mime.types;
  22. default_type application/octet-stream;
  23. ##
  24. # SSL Settings
  25. ##
  26. ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
  27. ssl_prefer_server_ciphers on;
  28. ##
  29. # Logging Settings
  30. ##
  31. access_log /var/log/nginx/access.log;
  32. error_log /var/log/nginx/error.log;
  33. ##
  34. # Gzip Settings
  35. ##
  36. gzip on;
  37. # gzip_vary on;
  38. # gzip_proxied any;
  39. # gzip_comp_level 6;
  40. # gzip_buffers 16 8k;
  41. # gzip_http_version 1.1;
  42. # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
  43. ##
  44. # Virtual Host Configs
  45. ##
  46. include /etc/nginx/conf.d/*.conf;
  47. include /etc/nginx/sites-enabled/*;
  48. }
  49. #mail {
  50. # # See sample authentication script at:
  51. # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
  52. #
  53. # # auth_http localhost/auth.php;
  54. # # pop3_capabilities "TOP" "USER";
  55. # # imap_capabilities "IMAP4rev1" "UIDPLUS";
  56. #
  57. # server {
  58. # listen localhost:110;
  59. # protocol pop3;
  60. # proxy on;
  61. # }
  62. #
  63. # server {
  64. # listen localhost:143;
  65. # protocol imap;
  66. # proxy on;
  67. # }
  68. #}
  1. ##
  2. # You should look at the following URL's in order to grasp a solid understanding
  3. # of Nginx configuration files in order to fully unleash the power of Nginx.
  4. # https://www.nginx.com/resources/wiki/start/
  5. # https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
  6. # https://wiki.debian.org/Nginx/DirectoryStructure
  7. #
  8. # In most cases, administrators will remove this file from sites-enabled/ and
  9. # leave it as reference inside of sites-available where it will continue to be
  10. # updated by the nginx packaging team.
  11. #
  12. # This file will automatically load configuration files provided by other
  13. # applications, such as Drupal or Wordpress. These applications will be made
  14. # available underneath a path with that package name, such as /drupal8.
  15. #
  16. # Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
  17. ##
  18. # Default server configuration
  19. #
  20. server {
  21. listen 80 default_server;
  22. listen [::]:80 default_server;
  23. # SSL configuration
  24. #
  25. # listen 443 ssl default_server;
  26. # listen [::]:443 ssl default_server;
  27. #
  28. # Note: You should disable gzip for SSL traffic.
  29. # See: https://bugs.debian.org/773332
  30. #
  31. # Read up on ssl_ciphers to ensure a secure configuration.
  32. # See: https://bugs.debian.org/765782
  33. #
  34. # Self signed certs generated by the ssl-cert package
  35. # Don't use them in a production server!
  36. #
  37. # include snippets/snakeoil.conf;
  38. root /var/www/html;
  39. # Add index.php to the list if you are using PHP
  40. index index.html index.htm index.nginx-debian.html;
  41. server_name _;
  42. location / {
  43. # First attempt to serve request as file, then
  44. # as directory, then fall back to displaying a 404.
  45. try_files $uri $uri/ =404;
  46. }
  47. # pass PHP scripts to FastCGI server
  48. #
  49. #location ~ \.php$ {
  50. # include snippets/fastcgi-php.conf;
  51. #
  52. # # With php-fpm (or other unix sockets):
  53. # fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
  54. # # With php-cgi (or other tcp sockets):
  55. # fastcgi_pass 127.0.0.1:9000;
  56. #}
  57. # deny access to .htaccess files, if Apache's document root
  58. # concurs with nginx's one
  59. #
  60. #location ~ /\.ht {
  61. # deny all;
  62. #}
  63. }
  64. # Virtual Host configuration for example.com
  65. #
  66. # You can move that to a different file under sites-available/ and symlink that
  67. # to sites-enabled/ to enable it.
  68. #
  69. #server {
  70. # listen 80;
  71. # listen [::]:80;
  72. #
  73. # server_name example.com;
  74. #
  75. # root /var/www/example.com;
  76. # index index.html;
  77. #
  78. # location / {
  79. # try_files $uri $uri/ =404;
  80. # }
  81. #}

用 nginx 搭建一个静态资源web 服务器

  1. server {
  2. listen 8080; # 监听8080 端口
  3. # '/' 表示所有的请求
  4. location / {
  5. # 下面的dir/ 带表的是 /etc/nginx/nginx.conf/dir/
  6. # 也可以用root, root会将url中的路径带到目录中来
  7. alias dir/
  8. }
  9. }
  • alias 用相对路径的相对位置是哪里呢? 通过 nginx -V 查看 link
  • root 和 alias 的区别是什么呢~
    • alias is used to replace the location part path (LPP) in the request path, while the root is used to be prepended to the request path.
    • link