编译安装

安装目录及参数规划

  • nginx安装目录:/usr/local/nginx
  • nginx配置文件目录:/usr/local/nginx/conf/nginx.conf
  • nginx虚拟服务器配置目录:/usr/local/nginx/conf/vhost/
  • log日志目录:/var/log/nginx/
  • pid文件目录:/var/run/nginx.pid
  • lock锁目录:/var/run/nginx.lock
  • 临时缓存目录:/var/cache/nginx
  • 站点目录:/www/wwwroot/
  • nginx运行用户名:nginx
  • nginx运行用户组:nginx

    安装依赖

    Linux环境下,安装 GCC编译器、正则表达式PCRE库、zlib压缩库、OpenSSL开发库 ``` yum install gcc gcc-c++
  1. Debian环境下安装:

apt-get -y update apt-get -y install curl wget perl unzip build-essential libmaxminddb-dev libgd-dev

  1. ### configure的命令参数
  2. - 列出configure包含的参数:`./configure --help`
  3. #### 通用配置选项解释
  4. |
  5. 选项
  6. | 解释
  7. |
  8. | --- | --- |
  9. |
  10. --prefix=PATH
  11. | Ngix 安装的根路径,所有其他的安装路径都要依赖于该选项
  12. |
  13. |
  14. --sbin-path=PATH
  15. | 指定口ginx 二进制文件的路径。如果没有指定,那么这个路径会 依赖于 prefix 选项
  16. |
  17. |
  18. --conf-path=PATH
  19. | 如果在命令行没有指定配置文件,那么将会通过这里指定的路径,nginx 将会去那里查找它的配置文件
  20. |
  21. |
  22. --error-log-path=PATH
  23. | 指定错误文件的路径,nginx 将会往其中写入错误日志文件,除非有其他的配置
  24. |
  25. |
  26. --pid-path=PATH
  27. | 指定的文件将会写入nginx master进程的pid通常卸载/var/run/目录下
  28. |
  29. |
  30. --lock-path=PATH
  31. | 共享储存器互斥锁文件的路径
  32. |
  33. |
  34. --user=USER
  35. | worker进程运行的用户
  36. |
  37. |
  38. --group=GROUP
  39. | worker进程运行的用户组
  40. |
  41. |
  42. --with-file-aio
  43. | FreeBSD 4.3+和linux 2.6.22+系统启用异步I/O
  44. |
  45. |
  46. --with-debug
  47. | 这个选项用于调试日志,在生产环境的系统中不推荐使用该选项
  48. |
  49. #### 临时路径配置选项
  50. |
  51. 选项
  52. | 解释
  53. |
  54. | --- | --- |
  55. |
  56. --error-log-path=PATH
  57. | 错误日志的默认路径
  58. |
  59. |
  60. --http-log-path=PATH
  61. | http 访问日志的默认路径
  62. |
  63. |
  64. --http-client-body-temp-path=PATH
  65. | 从客户端收到请求后,该选项设置的目录用于作为请求体 临时存放的目录。如果 WebDAV 模块启用,那么推荐设置 该路径为同 一文件系统上的目录作为最终的目的地
  66. |
  67. |
  68. --http-proxy-temp-path=PATH
  69. | 在使用代理后,通过该选项设置存放临时文件路径
  70. |
  71. |
  72. --http-fastcgi-temp-path=PATH
  73. | 设置 FastCGI 临时文件的目录
  74. |
  75. |
  76. --http-uwsgi-temp-path=PATH
  77. | 设置 uWSG工临时文件的目录
  78. |
  79. |
  80. --http-scgi-temp-path=PATH
  81. | 设置 SCGII临时文件的目录
  82. |
  83. #### PCRE的配置参数
  84. |
  85. 选项
  86. | 解释
  87. |
  88. | --- | --- |
  89. |
  90. --without-pcre
  91. | 如果确定Nginx不用解析正则表达式,那么可以使用这个参数
  92. |
  93. |
  94. --with-pcre
  95. | 强制使用PCRE
  96. |
  97. |
  98. --with-pcre=DIR
  99. | 指定PCRE库的源码位置,在编译nginx时会进入该目录编译PCRE源码
  100. |
  101. |
  102. --with-pcre-opt=OPTIONS
  103. | 编译PCRE源码是希望加入的编译选项
  104. |
  105. #### OpenSSL的配置参数
  106. |
  107. 选项
  108. | 解释
  109. |
  110. | --- | --- |
  111. |
  112. --with-openssl=DIR
  113. | 指定OpenSSL库的源码位置,在编译nginx时会进入该目录编译OpenSSL。如果web服务器需要使用HTTPS,那么Nginx要求必须使用OpenSSL
  114. |
  115. |
  116. --with-openssl-opt=OPTIONS
  117. | 编译OpenSSL源码时希望加入的编译选项
  118. |
  119. #### zlib的配置参数
  120. |
  121. 选项
  122. | 解释
  123. |
  124. | --- | --- |
  125. |
  126. --with-zlib=DIR
  127. | 指定zlib库的源码位置,在编译nginx时会进入该目录编译zlib。如果需要使用gzip压缩就必须要zlib库的支持
  128. |
  129. |
  130. --with-zlib-opt=OPTIONS
  131. | 编译zlib源码时希望加入的编译选项
  132. |
  133. |
  134. --with-zlib-asm=CPU
  135. | 指定对特定的CPU使用zlib库的汇编优化功能,目前支持两种架构:pentiumpentiumpro
  136. |
  137. ### Nginx编译步骤
  138. 1、创建用户与用户组

groupadd nginx useradd -M -g nginx nginx -s /sbin/nologin

  1. 2、编译 openssl<br />安装 openssl

openssl_version=’1.1.1g’

cd /usr/local wget —no-check-certificate -O openssl.tar.gz https://www.openssl.org/source/openssl-1.1.1g.tar.gz tar -zxvf openssl.tar.gz cd openssl-1.1.1g ./config shared —openssldir=/usr/local/openssl —prefix=/usr/local/openssl make && make install

echo “/usr/local/lib64/“ >> /etc/ld.so.conf ldconfig

openssl version

  1. 3、下载nginx源码包,解压并进入nginx源码根目录

nginx_version=’1.19.9’

cd /usr/local wget http://nginx.org/download/nginx-${nginx_version}.tar.gz tar -zxvf nginx-${nginx_version}.tar.gz cd nginx-${nginx_version}

  1. 4、生成makefile文件

mkdir /var/cache/nginx

./configure \ —prefix=/usr/local/nginx \ —sbin-path=/usr/local/nginx/sbin/nginx \ —conf-path=/usr/local/nginx/conf/nginx.conf \ —error-log-path=/var/log/nginx/error.log \ —http-log-path=/var/log/nginx/access.log \ —pid-path=/var/run/nginx.pid \ —lock-path=/var/run/nginx.lock \ —http-client-body-temp-path=/var/cache/nginx/client_temp \ —http-proxy-temp-path=/var/cache/nginx/proxy_temp \ —http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ —http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ —http-scgi-temp-path=/var/cache/nginx/scgi_temp \ —user=nginx \ —group=nginx \ —with-file-aio \ —with-threads \ —with-http_addition_module \ —with-http_auth_request_module \ —with-http_dav_module \ —with-http_flv_module \ —with-http_gunzip_module \ —with-http_gzip_static_module \ —with-http_mp4_module \ —with-http_random_index_module \ —with-http_realip_module \ —with-http_secure_link_module \ —with-http_slice_module \ —with-http_ssl_module \ —with-http_stub_status_module \ —with-http_sub_module \ —with-http_v2_module \ —with-mail \ —with-mail_ssl_module \ —with-stream \ —with-stream_realip_module \ —with-stream_ssl_module \ —with-stream_ssl_preread_module \ —with-openssl=../openssl

  1. 5、编译与安装

make -j4 && make -j4 install

  1. 6、安装成功执行以下命令查看nginx版本号

nginx -v

  1. 查看编译参数和模块:

nginx -V

  1. ### 编译其他可选模块
  2. 1、编译Lua
  3. - 下载安装luaji:[http://luajit.org/download/LuaJIT-2.0.2.tar.gz](http://luajit.org/download/LuaJIT-2.0.2.tar.gz)
  4. - ngx_devel_kit下载:[https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz](https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.tar.gz)
  5. - lua-nginx-module下载:[https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz](https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz)
  6. 安装:

cd /usr/local wget http://luajit.org/download/LuaJIT-2.0.2.tar.gz tar -axv -f LuaJIT-2.0.2.tar.gz cd LuaJIT-2.0.2 make install PREFIX=/usr/local/LuaJIT export LUAJIT_LIB=/usr/local/LuaJIT/lib export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0

wget https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1rc1.zip wget https://github.com/openresty/lua-nginx-module/archive/v0.10.9rc7.tar.gz tar -axf v0.3.1rc1.tar.gz tar -zxf v0.10.9rc7.tar.gz mv lua-nginx-module-0.10.9rc7/ lua-nginx-module mv ngx_devel_kit-0.3.1rc1/ ngx_devel_kit

重新生成makefile加入lua-module和lua-devel

cd /usr/local/nginx-${nginx_version} ./configure \ —prefix=/usr/local/nginx \ —sbin-path=/usr/sbin/nginx \ —conf-path=/usr/local/nginx/conf/nginx.conf \ —error-log-path=/var/log/nginx/error.log \ —http-log-path=/var/log/nginx/access.log \ —pid-path=/var/run/nginx.pid \ —lock-path=/var/run/nginx.lock \ —http-client-body-temp-path=/var/cache/nginx/client_temp \ —http-proxy-temp-path=/var/cache/nginx/proxy_temp \ —http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \ —http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \ —http-scgi-temp-path=/var/cache/nginx/scgi_temp \ —user=nginx \ —group=nginx \ —with-file-aio \ —with-threads \ —with-http_addition_module \ —with-http_auth_request_module \ —with-http_dav_module \ —with-http_flv_module \ —with-http_gunzip_module \ —with-http_gzip_static_module \ —with-http_mp4_module \ —with-http_random_index_module \ —with-http_realip_module \ —with-http_secure_link_module \ —with-http_slice_module \ —with-http_ssl_module \ —with-http_stub_status_module \ —with-http_sub_module \ —with-http_v2_module \ —with-mail \ —with-mail_ssl_module \ —with-stream \ —with-stream_realip_module \ —with-stream_ssl_module \ —with-stream_ssl_preread_module \ —add-module=../ngx_devel_kit \ —add-module=../lua-nginx-module \ —with-ld-opt=”-Wl,-rpath,$LUAJIT_LIB”

编译安装

make -j2 && make install

nginx -V

  1. `nginx -V`出来能看到 `--add-module=./ngx_devel_kit --add-module=./lua-nginx-module`说明lua模块编译安装成功了。<br />2、编译 purecache

cd /usr/local && wget http://soft.xiaoz.org/nginx/ngx_cache_purge-2.3.tar.gz tar -zxvf ngx_cache_purge-2.3.tar.gz mv ngx_cache_purge-2.3 ngx_cache_purge

  1. nginx目录,重新生成makefile加入ngx_cache_purge模块:

—add-module=../ngx_cache_purge

  1. 3、编译安装 brotli

cd /usr/local wget http://soft.xiaoz.org/nginx/ngx_brotli.tar.gz tar -zxvf ngx_brotli.tar.gz

  1. nginx目录,重新生成makefile加入ngx_cache_purge模块:

—add-module=../ngx_brotli

  1. 4、编译pcre<br />安装 pcre

cd /usr/local wget —no-check-certificate https://ftp.pcre.org/pub/pcre/pcre-${pcre_version}.tar.gz tar -zxvf pcre-8.43.tar.gz mv pcre-8.43 pcre cd pcre ./configure make -j4 && make -j4 install

  1. nginx目录,重新生成makefile加入pcre模块:

—with-pcre=../pcre \ —with-pcre-jit \

  1. 5、编译 zlib<br />安装zlib

cd /usr/local wget http://soft.xiaoz.org/linux/zlib-1.2.11.tar.gz tar -zxvf zlib-1.2.11.tar.gz cd zlib-1.2.11 ./configure make -j4 && make -j4 install

  1. nginx目录,重新生成makefile加入zlib模块:

—with-zlib=../zlib-1.2.11 \

  1. ### 编译之后的配置
  2. 清理安装文件:

cd /usr/local rm -rf zlib-1. rm -rf pcre-8. rm -rf ngx_cache_purge rm -rf ngx_brotli

  1. 备份 nginx.conf

mv /usr/local/nginx/conf/nginx.conf /usr/local/nginx/conf/nginx.conf.bak mkdir -p /usr/local/nginx/conf/vhost

  1. 修改 nginx.conf

user nginx nginx; worker_processes auto; worker_rlimit_nofile 50000; error_log logs/error.log;

error_log logs/error.log notice;

error_log logs/error.log info;

pid /var/run/nginx.pid;

events { use epoll; worker_connections 51200;

  1. #worker_connections 1024;
  2. multi_accept on;

}

http { include mime.types; default_type application/octet-stream; server_names_hash_bucket_size 128; client_header_buffer_size 32k; large_client_header_buffers 4 32k; client_max_body_size 1024m; client_body_buffer_size 10m; sendfile on; tcp_nopush on; keepalive_timeout 120; server_tokens off; tcp_nodelay on; proxy_headers_hash_max_size 51200; proxy_headers_hash_bucket_size 6400;

  1. #开启Brotli压缩
  2. #brotli on;
  3. #brotli_comp_level 6;
  4. #最小长度
  5. #brotli_min_length 512;
  6. #brotli_types text/plain text/javascript text/css text/xml text/x-component application/javascript application/x-javascript application/xml application/json application/xhtml+xml application/rss+xml application/atom+xml application/x-font-ttf application/vnd.ms-fontobject image/svg+xml image/x-icon font/opentype;
  7. #brotli_static always;
  8. gzip on;
  9. gzip_buffers 16 8k;
  10. gzip_comp_level 6;
  11. gzip_http_version 1.1;
  12. gzip_min_length 256;
  13. gzip_proxied any;
  14. gzip_vary on;
  15. gzip_types
  16. text/xml application/xml application/atom+xml application/rss+xml application/xhtml+xml image/svg+xml
  17. text/javascript application/javascript application/x-javascript
  18. text/x-json application/json application/x-web-app-manifest+json
  19. text/css text/plain text/x-component
  20. font/opentype application/x-font-ttf application/vnd.ms-fontobject
  21. image/x-icon;
  22. gzip_disable "MSIE [1-6]\.(?!.*SV1)";
  23. #If you have a lot of static files to serve through Nginx then caching of the files' metadata (not the actual files' contents) can save some latency.
  24. open_file_cache max=1000 inactive=20s;
  25. open_file_cache_valid 30s;
  26. open_file_cache_min_uses 2;
  27. open_file_cache_errors on;
  28. #limit connection
  29. limit_conn_zone $binary_remote_addr zone=addr:10m;
  30. server {
  31. listen 80;
  32. server_name localhost;
  33. #charset koi8-r;
  34. #access_log logs/host.access.log main;
  35. location / {
  36. root html;
  37. index index.html index.htm;
  38. }
  39. #error_page 404 /404.html;
  40. # redirect server error pages to the static page /50x.html
  41. #
  42. error_page 500 502 503 504 /50x.html;
  43. location = /50x.html {
  44. root html;
  45. }
  46. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  47. #
  48. #location ~ \.php$ {
  49. # proxy_pass http://127.0.0.1;
  50. #}
  51. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  52. #
  53. #location ~ \.php$ {
  54. # root html;
  55. # fastcgi_pass 127.0.0.1:9000;
  56. # fastcgi_index index.php;
  57. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  58. # include fastcgi_params;
  59. #}
  60. # deny access to .htaccess files, if Apache's document root
  61. # concurs with nginx's one
  62. #
  63. #location ~ /\.ht {
  64. # deny all;
  65. #}
  66. }
  67. # another virtual host using mix of IP-, name-, and port-based configuration
  68. #
  69. #server {
  70. # listen 8000;
  71. # listen somename:8080;
  72. # server_name somename alias another.alias;
  73. # location / {
  74. # root html;
  75. # index index.html index.htm;
  76. # }
  77. #}
  78. # HTTPS server
  79. #
  80. #server {
  81. # listen 443 ssl;
  82. # server_name localhost;
  83. # ssl_certificate cert.pem;
  84. # ssl_certificate_key cert.key;
  85. # ssl_session_cache shared:SSL:1m;
  86. # ssl_session_timeout 5m;
  87. # ssl_ciphers HIGH:!aNULL:!MD5;
  88. # ssl_prefer_server_ciphers on;
  89. # location / {
  90. # root html;
  91. # index index.html index.htm;
  92. # }
  93. #}
  94. include vhost/*.conf;

}

  1. 设置环境变量:

echo “export PATH=$PATH:/usr/local/nginx/sbin” >> /etc/profile export PATH=$PATH:’/usr/local/nginx/sbin’

  1. 安装服务:

cat >> /etc/systemd/system/nginx.service <<EOF [Unit] Description=nginx - high performance web server Documentation=http://nginx.org/en/docs/ After=network.target

[Service] Type=forking PIDFile=/var/run/nginx.pid ExecStartPost=/bin/sleep 0.1 ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf ExecReload=/bin/kill -s HUP $MAINPID ExecStop=/bin/kill -s QUIT $MAINPID LimitNOFILE=1000000 LimitNPROC=1000000 LimitCORE=1000000

[Install] WantedBy=multi-user.targe EOF

  1. 设置开机启动:

systemctl daemon-reload systemctl enable nginx

  1. 启动:

/usr/local/nginx/sbin/nginx

  1. 设置日志文件切割:

cat >> /etc/logrotate.d/nginx <<EOF /data/wwwlogs/*nginx.log { daily rotate 5 missingok dateext compress notifempty sharedscripts postrotate [ -e /var/run/nginx.pid ] && kill -USR1 cat /var/run/nginx.pid endscript } EOF

  1. 卸载

杀掉nginx进程

pkill nginx

删除nginx用户

userdel nginx && groupdel nginx

备份一下配置

cp -a /usr/local/nginx/conf/vhost /home/vhost_bak

删除目录

rm -rf /usr/local/nginx sed -i “s%:/usr/local/nginx/sbin%%g” /etc/profile

删除自启

sed -i ‘/^.*nginx/d’ /etc/rc.d/rc.local rm -rf /etc/systemd/system/nginx.service

删除日志分割

rm -rf /etc/logrotate.d/nginx

  1. ## docker compose安装
  2. 创建目录:

mkdir -p /data/docker/nginx/wwwroot/html80

mkdir -p /data/docker/nginx/wwwroot/html8080

mkdir -p /data/docker/nginx/conf

  1. 修改 `/data/docker/nginx/conf/nginx.conf` 目录下的 nginx.conf

version: ‘3.1’ services: nginx: restart: always image: nginx container_name: nginx ports:

  1. - 4180:80
  2. - 4181:8080
  3. volumes:
  4. - /data/docker/nginx/conf/nginx.conf:/etc/nginx/nginx.conf
  5. - /data/docker/nginx/wwwroot:/usr/share/nginx/wwwroot
  1. nginx.conf

worker_processes 1;

events { worker_connections 1024; }

http { include mime.types; default_type application/octet-stream;

  1. sendfile on;
  2. keepalive_timeout 65;
  3. server {
  4. listen 80;
  5. server_name static.cshop.com;
  6. # 所有的请求都以 / 开始,所有的请求都可以匹配此 location
  7. location / {
  8. root /usr/share/nginx/wwwroot/html80;
  9. index index.html index.htm;
  10. }
  11. }
  12. server {
  13. listen 8080;
  14. server_name admin.cshop.com;
  15. location / {
  16. root /usr/share/nginx/wwwroot/html8080;
  17. index index.html index.htm;
  18. }
  19. }

}

  1. 配置hosts

192.168.56.100 admin.cshop.com

192.168.56.100 static.cshop.com

  1. ## 语法
  2. Location 语法:

location [=||*|^~] /uri/ { … }

  1. 说明:
  2. |
  3. 规则
  4. | 说明
  5. | 例子
  6. |
  7. | --- | --- | --- |
  8. |
  9. =
  10. | 精准匹配
  11. | location = /api/list
  12. |
  13. |
  14. ~
  15. | 正则匹配(区分大小写),支持正则
  16. | location ~ /api/
  17. |
  18. |
  19. ~*
  20. | 正则匹配(**不** 区分大小写)
  21. | location ~* /api/
  22. |
  23. |
  24. !~
  25. | 正则不匹配(区分大小写)
  26. | location !~ /api/
  27. |
  28. |
  29. !~*
  30. | 正则不匹配(**不** 区分大小写)
  31. | location !~* /api/
  32. |
  33. |
  34. ^~
  35. | 字符串匹配(区分大小写),优先级高于正则
  36. | location ^~ /api/
  37. |
  38. |
  39. /
  40. | 通用匹配
  41. | location /
  42. |
  43. 查找顺序和优先级
  44. -
  45. 带有“=“的精确匹配优先
  46. -
  47. 没有修饰符的精确匹配
  48. -
  49. 正则表达式按照他们在配置文件中定义的顺序
  50. -
  51. 带有“^~”修饰符的,开头匹配
  52. -
  53. 带有“” 或“*” 修饰符的,如果正则表达式与URI匹配
  54. -
  55. 没有修饰符的,如果指定字符串与URI开头匹配
  56. 例子:

server { listen 80; server_name abc.com; access_log “pipe:rollback /data/log/nginx/access.log interval=1d baknum=7 maxsize=1G” main; location ^~/user/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://user/; } location ^~/order/ { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://order/; } }

`` ^~/user/表示匹配前缀是user的请求,proxy_pass的结尾有/, 则会把/user/*后面的路径直接拼接到后面,即移除user`.