nginx 下载

http://nginx.org/en/download.html
直接下载地址(2021年1月 . 稳定版)
http://nginx.org/download/nginx-1.18.0.tar.gz
下载到 nginx 目录

  1. cd /usr/local/
  2. mkdir nginx
  3. cd nginx
  4. wget http://nginx.org/download/nginx-1.18.0.tar.gz

解压缩文件

  1. tar -xvf nginx-1.18.0.tar.gz
  2. cd nginx-1.18.0

需要检查已安装了 nginx 的相关依赖,如:gcc、pcre、pcre-devel、zlib、zlib-devel、openssl、openssl-devel

安装依赖

  1. yum install -y libxslt-devel.x86_64 gd.x86_64 gd-devel.x86_64 perl-devel perl-ExtUtils-Embed GeoIP.x86_64 GeoIP-devel.x86_64 gperftools.x86_64 gperftools-devel.x86_64 pcre-devel.x86_64 openssl-devel.x86_64

编译安装(configure 配置根据需要而定)

  • 注意:多个nginx 并存时,配置参数文件名和路径不能重复
  • 执行之前请先创建所需文件目录和用户组,详情看下面的相关问题

    编译安装的nginx需要指定编译参数,开启需要使用的模块,否则包括ssh等都无法使用。 nginx 默认安装位置是 “/usr/local/nginx” 目录

  1. ./configure --prefix=/usr/local/nginx --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-compat --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-debug --with-http_geoip_module
  2. 下一步
  3. make
  4. make install

./configure 配置项比较多,详情参考 http://nginx.org/en/docs/configure.html —prefix= 定义将保留服务器文件的目录,默认 /usr/local/nginx —sbin-path= 设置nginx可执行文件的名称。默认文件名为 prefix/sbin/nginx —modules-path= 定义安装nginx动态模块的目录(和nginx模块(如—with-http_dav_module)不是一个概念)。默认情况下使用prefix/modules目录。 —conf-path= 配置文件生成目录,默认是 prefix/conf/nginx.conf —error-log-path= 设置主要错误、警告和诊断文件的名称。默认 prefix/logs/error.log —http-log-path= 设置HTTP服务器的主请求日志文件的名称。默认为prefix/logs/access.log —pid-path= 用来存储主进程的进程ID,默认 prefix/logs/nginx.pid —lock-path= 为锁定文件的名称设置前缀,默认 prefix/logs/nginx.lock —user=name= 工作进程的用户名称,默认是 nobody —group=name 工作进程的用户组名称,默认和用户名称一致 nobody —http-client-body-temp-path=/var/cache/nginx/client_body_temp 用于存储存放客户端请求体的临时文件的目录。默认prefix/client_body_temp —http-proxy-temp-path=/var/cache/nginx/proxy_temp 存储带有从代理服务器接收到的数据的临时文件。默认prefix/proxy_temp
—http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp 存储从FastCGI服务器接收到的临时文件。默认prefix/fastcgi_temp —http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp 存储带有从uwsgi服务器接收的数据的临时文件。默认prefix/uwsgi_temp —http-scgi-temp-path=/var/cache/nginx/scgi_temp 存储带有从SCGI服务器接收到的数据的临时文件。默认prefix/scgi_temp —with-compat 启用动态模块兼容性 —with-file-aio 启用异步文件 IO —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 https 支持 —with-http_stub_status_module 此模块可以查看nginx对数据包处理的基本信息 —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-debug —with-http_geoip_module

一些其他自带的模块

详情清单 https://docs.nginx.com/nginx/admin-guide/installing-nginx/installing-nginx-open-source/#modules-built-by-default

ngx_http_limit_conn_module 用于限制并发连接数,而 ngx_http_limit_req_module 则用于限制并发请求数。实际业务场景中,通常使用 ngx_http_limit_req_module 会更多一些。自带

./configure 配置编译完成
image.png
make & make install 完成后的目录结构
Snipaste_2020-12-15_14-36-48.png
安装完成之后,我们可以通过如下命令,获取当前安装 Nginx 时设定的编译配置。

  1. /usr/local/nginx/sbin/nginx -V

相关问题

相关问题:运行 nginx 时提示:nginx: [emerg] getpwnam(“nginx”) failed
是因为安装 nginx 时候指定了 —user=nginx —group=nginx 用户和组,而系统中是没有创建 nginx 用户的,需要创建后在运行。

  1. groupadd nginx
  2. useradd -s /sbin/nologin -g nginx -M nginx
  3. chown -R nginx:nginx /usr/local/nginx

相关问题:运行 nginx 时提示:mkdir() “/var/cache/nginx/client_temp” failed (2: No such file or directory)
是因为安装 nginx 的时候指定了系列缓存目录到 /var/cache/nginx/…… 目录,而 /var/cache 目录中没有创建 nginx 目录导致

  1. cd /var/cache
  2. mkdir nginx
  3. chown -R nginx:nginx nginx

模块添加(可选)

常用模块编译配置
如果在编译配置时候,某些模块没有配置的,可以进入源码包,重新执行编译,如缺少 https 支持

  1. cd /usr/local/nginx/nginx-1.18.0/
  2. ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
  3. make

重新安装新的模块以后,将源码目录中(/usr/local/nginx/nginx-1.18.0/objs )的 nginx 执行程序复制替换掉到 (/usr/local/nginx/sbin)下。
make 完之后在源码包 objs 目录下就多了个 nginx,这个就是新版本的程序了。把新的 nginx程序覆盖旧的即可。
如果是刚安装好的 nginx,可以执行 make install 覆盖安装

  1. make install

此操作会覆盖掉原有的所有文件,相当于重新配置安装。

启动服务并运行

  1. ./nginx 启动服务
  2. ./nginx -s stop 立即停止服务(强制停止服务)
  3. ./nginx -s quit 优雅的停止服务(先关闭监听端口,停止接收新的连接,然后把当前正在处理的连接全部处理完,最后退出进程)
  4. ./nginx -s reopen 日志文件回滚
  5. ./nginx -s reload 先检查新的配置项是否正确,然后以quit方式关闭,再重启
  6. ./nginx -c /tmp/nginx.conf 指定配置文件的启动方式
  7. ps aux|grep nginx 查看nginx进程
  1. nginx 启动、停止、重新加载配置
  2. -------------------------------
  3. 执行 nginx 命令启动 nginx。启动 nginx 之后,可使用如下命令:
  4. nginx -s stop 快速关闭 nginx
  5. nginx -s quit 优雅的关闭 nginx
  6. nginx -s reload 重新加载配置
  7. nginx -s reopen 重新打开日志文件
  8. 优雅地关闭 nginx,这是说 nginx 主进程会等待 worker 进程完成当前用户请求的处理。
  9. 执行:nginx -s quit (以启动 nginx 时的用户身份执行该命令)
  10. 更改配置之后,必须执行重新加载配置的命令,或者重新启动 nginx,以使得配置生效:
  11. nginx -s reload
  12. 当主进程接收到 reload 信号,它会检查配置文件的语法,然后尝试应用该配置。如果成功,主进程
  13. 启动新的 worker 进程,并发送消息给原来的 worker 进程要求他们关闭。如果加载配置失败,主进程
  14. 会对改动进行回滚,继续以原来的配置进行工作。当原来的 worker 进程接收到消息要他们关闭,他们会
  15. 停止接收新的连接,并继续处理当前的请求直到完成。完成之后,原来的 worker 进程将会退出。

设置开机自启动

官方提供了方法:将以下内容保存为 /lib/systemd/system/nginx.service 然后 systemctl enable nginx 即可
https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

  1. cd /lib/systemd/system
  2. vi nginx.service
  1. [Unit]
  2. Description=The NGINX HTTP and reverse proxy server
  3. After=syslog.target network-online.target remote-fs.target nss-lookup.target
  4. Wants=network-online.target
  5. [Service]
  6. Type=forking
  7. PIDFile=/usr/local/nginx/logs/nginx.pid
  8. ExecStartPre=/usr/local/nginx/sbin/nginx -t
  9. ExecStart=/usr/local/nginx/sbin/nginx
  10. ExecReload=/usr/local/nginx/sbin/nginx -s reload
  11. ExecStop=/bin/kill -s QUIT $MAINPID
  12. PrivateTmp=true
  13. [Install]
  14. WantedBy=multi-user.target

开启开机自启动

  1. systemctl enable nginx

ngxtop 流量实时监测

https://github.com/lebinh/ngxtop

ngxtop 解析 nginx 访问日志,并输出类似 top 服务器的有用指标。因此,可以实时了解服务器的状况。 ngxtop主要是使用python2开发和测试的,但也支持python3。

安装使用

  1. pip install ngxtop

查看实时日志分析

  1. ngxtop -l /usr/local/nginx/logs/access.log
  2. ngxtop top remote_addr -l /usr/local/nginx/logs/access.log
  3. ngxtop -i 'status >= 400' print request status http_referer

参数 -l 日志文件目录

Snipaste_2020-12-15_07-13-57.png

nginx 配置

安装后的 nginx.conf 文件

  1. # 默认是不启用的,安装时候已经指定了 user=nginx,此处注释即可
  2. #user nobody;
  3. # 工作进程数,一般根据 CPU的核心线程数定(4核写 4,一般和核心数一致),默认是 1。
  4. # 查看 CPU 核心数量: grep processor /proc/cpuinfo | wc -l
  5. worker_processes 1;
  6. # 错误日志输出目录,安装时已经配置过,忽略。
  7. #error_log logs/error.log;
  8. #error_log logs/error.log notice;
  9. #error_log logs/error.log info;
  10. # 进程文件,忽略
  11. #pid logs/nginx.pid;
  12. events {
  13. # Nginx可以同时为多少人提供服务。默认值为 1024。可以调整到: 核心数 * 1024
  14. worker_connections 1024; # 可调整到 2048+
  15. }
  16. http {
  17. include mime.types; # 解析请求Type,设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
  18. default_type application/octet-stream; # 默认 忽略
  19. # 按照指定的格式写入日志文件
  20. #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  21. # '$status $body_bytes_sent "$http_referer" '
  22. # '"$http_user_agent" "$http_x_forwarded_for"';
  23. # 日志文件路径,安装时候已经配置过。忽略
  24. #access_log logs/access.log main;
  25. # 指定是否使用sendfile系统调用来传输文件。
  26. # 避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。
  27. # Nginx 是一个静态文件服务器的时候,开启 SENDFILE 配置项能大大提高 Nginx 的性能。 => 文件句柄
  28. # Nginx 是一个反向代理来使用的时候,SENDFILE 则没什么用了。 => socket
  29. sendfile on;
  30. # 数据达到一定量后发送。对于 www,ftp 等大文件很有帮助。忽略
  31. # 使用sendfile函数时,tcp_nopush才起作用,它和指令tcp_nodelay是互斥的。
  32. #tcp_nopush on;
  33. # 客户端连接超时时间。单位秒。保持默认:65
  34. # 调小会导致上传操作可能无法完成;调大点的话,许多无效的http连接占据着nginx的连接数!
  35. #keepalive_timeout 0;
  36. keepalive_timeout 65;
  37. # 开启和关闭gzip模式
  38. # 对于纯 api 应用,无需设置。
  39. # 例如图片/mp3这样的二进制文件也不必压缩;因为压缩率比较小, 比如100->80字节,而且压缩也是耗费CPU资源的.
  40. # 如果需要访问大量的静态文件,需要配置以提高传输性能。
  41. #gzip on;
  42. # server 节点可以存在多个,不同端口,不同域名都可以单独配置
  43. server {
  44. # 监听 80端口
  45. listen 0;
  46. # 可以是IP地址,也可以是通配符域名或者域名:18.18.18.18; 或 www.*; 或 *.domain.com; 或 domain.com www.domain.com;
  47. server_name localhost;
  48. # 字符集,不开启。
  49. # 如果开启可以设置: charset utf-8; 或 charset utf-8,gbk;
  50. #charset koi8-r;
  51. # 是否需要单独产生日志到文件。
  52. # 一般情况下不需要
  53. #access_log logs/host.access.log main;
  54. # 来自 80 端口的所有请求定位到 html 目录下
  55. location / {
  56. root html;
  57. index index.html index.htm;
  58. }
  59. # 404 处理
  60. #error_page 404 /404.html;
  61. # redirect server error pages to the static page /50x.html
  62. # 将服务器错误页面重定向到静态页面,响应状态码:500 502 503 504 等
  63. error_page 500 502 503 504 /50x.html; # 访问下方的 location
  64. location = /50x.html {
  65. root html;
  66. }
  67. # PHP相关的,我大 java 用不到。删之
  68. # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  69. #
  70. #location ~ \.php$ {
  71. # proxy_pass http://127.0.0.1;
  72. #}
  73. # PHP相关的,我大 java 用不到。删之
  74. # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
  75. #
  76. #location ~ \.php$ {
  77. # root html;
  78. # fastcgi_pass 127.0.0.1:9000;
  79. # fastcgi_index index.php;
  80. # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
  81. # include fastcgi_params;
  82. #}
  83. # 忽略。.htaccess 根据目录配置规则,如缓存,不可访问等
  84. # deny access to .htaccess files, if Apache's document root
  85. # concurs with nginx's one
  86. #
  87. #location ~ /\.ht {
  88. # deny all;
  89. #}
  90. }
  91. # another virtual host using mix of IP-, name-, and port-based configuration
  92. # 另一个虚拟主机混合使用基于IP、名称和端口的配置
  93. #server {
  94. # listen 8000;
  95. # listen somename:8080;
  96. # server_name somename alias another.alias;
  97. # location / {
  98. # root html;
  99. # index index.html index.htm;
  100. # }
  101. #}
  102. # HTTPS server
  103. # 另一个基于 https 的主机
  104. #server {
  105. # listen 443 ssl;
  106. # server_name localhost;
  107. # ssl_certificate cert.pem;
  108. # ssl_certificate_key cert.key;
  109. # ssl_session_cache shared:SSL:1m;
  110. # ssl_session_timeout 5m;
  111. # ssl_ciphers HIGH:!aNULL:!MD5;
  112. # ssl_prefer_server_ciphers on;
  113. # location / {
  114. # root html;
  115. # index index.html index.htm;
  116. # }
  117. #}
  118. }

对默认的 nginx.conf 配置做优化和调整以后:

  1. # 默认是不启用的,安装时候已经指定了 user=nginx,此处注释即可
  2. #user nobody;
  3. # 工作进程数,一般根据 CPU的核心线程数定(4核写 4,一般和核心数一致),默认是 1。
  4. # 查看 CPU 核心数量: grep processor /proc/cpuinfo | wc -l
  5. worker_processes 4;
  6. # 错误日志输出目录,安装时已经配置过,忽略。此处会覆盖配置,并且可以按照日志等级输出
  7. #error_log logs/error.log;
  8. #error_log logs/error.log notice;
  9. #error_log logs/error.log info;
  10. # 进程文件路径
  11. pid /usr/local/nginx/logs/nginx.pid;
  12. events {
  13. # Nginx可以同时为多少人提供服务。 可以调整到: 核心数 * 1024 Default: 1024
  14. worker_connections 2048; # 可调整到 2048+
  15. }
  16. http {
  17. include mime.types; # 解析请求Type,设置HTTP Response的Content-Type,然后浏览器根据Content-Type的值处理文件。
  18. default_type application/octet-stream; # 默认 忽略
  19. # 日志文件路径,安装时候已经配置过。忽略
  20. #access_log logs/access.log main;
  21. # 指定是否使用sendfile系统调用来传输文件。
  22. # 避免了数据在内核缓冲区和用户缓冲区之间的拷贝,操作效率很高,被称之为零拷贝。
  23. # Nginx 是一个静态文件服务器的时候,开启 SENDFILE 配置项能大大提高 Nginx 的性能。 => 文件句柄
  24. # Nginx 是一个反向代理来使用的时候,SENDFILE 则没什么用了。 => socket
  25. sendfile on;
  26. # 客户端标头的大小。默认 1K。可单独配置在 server 节点中
  27. client_header_buffer_size 2k;
  28. # 大型客户端标头的缓冲区的最大数量和大小。默认 4 4k; 可单独配置在 server 节点中
  29. #large_client_header_buffers 10 10k;
  30. # 客户端缓冲区的大小。默认 128K。可单独配置在 server 节点中
  31. # client_body_buffer_size 256K;
  32. # 客户端 body 内容最大允许大小。超过最大大小,则Nginx将吐出413。 默认 10m。可单独配置在 server 节点中
  33. client_max_body_size 50m;
  34. # 客户端连接超时时间。单位秒。保持默认:65
  35. # 调小会导致上传操作可能无法完成;调大点的话,许多无效的http连接占据着nginx的连接数!可单独配置在 server 节点中
  36. #keepalive_timeout 0;
  37. keepalive_timeout 35;
  38. # 网上有 WebSocket 连接不上,并且报错:Handshake failed due to invalid Upgrade header: null
  39. # 目前本人没有遇到的,解决此问题可以添加配置
  40. # 在 nginx.conf 的 http 模块添加如下内容即可
  41. #map $http_upgrade $connection_upgrade {
  42. # default upgrade;
  43. # '' close;
  44. #}
  45. # 基于 server 的配置文件以单独的文件加载引入。统一放到配置文件目录 conf/vhost 中。以 domain.com.conf 方式命名
  46. # 便于以后修改更新
  47. include vhost/*.conf;
  48. }

简单版

  1. worker_processes 4;
  2. pid /usr/local/nginx/logs/nginx.pid;
  3. events {
  4. worker_connections 2048;
  5. }
  6. http {
  7. include mime.types;
  8. default_type application/octet-stream;
  9. sendfile on;
  10. client_header_buffer_size 2k;
  11. client_max_body_size 50m;
  12. keepalive_timeout 35;
  13. map $http_upgrade $connection_upgrade {
  14. default upgrade;
  15. '' close;
  16. }
  17. include vhost/*.conf;
  18. }

配置完成以后,创建 vhost 目录,基于域名的配置文件。以 domain.com 为例

  1. cd /usr/local/nginx/conf
  2. mkdir vhost
  3. cd vhost
  4. vi domain.com.conf

配置 domain.com.conf

如果指定了错误跳转页面,需要在 html 目录中创建 error.html 404.html

  1. # 强制使用 https 协议。将访问域名指向 https
  2. server {
  3. listen 80;
  4. # 服务器名称,可以是 IP地址或域名。
  5. server_name domain.com www.domain.com;
  6. # 重定向到 https
  7. # 正确写法是: return 301 https://$server_name$request_uri;
  8. # 此处 单个域名对应单个 ssl 证书。所以 $server_name 写死成 www.domain.com
  9. return 301 https://www.domain.com$request_uri;
  10. }
  11. server {
  12. listen 443 ssl;
  13. server_name domain.com www.domain.com;
  14. ssl_certificate /usr/local/nginx/CA/domain.com/domain.com.pem;
  15. ssl_certificate_key /usr/local/nginx/CA/domain.com/domain.com.key;
  16. ssl_session_timeout 5m;
  17. # 默认 HIGH:!RC4:!3DES:!aDSS:!aNULL:!kPSK:!kSRP:!MD5:@STRENGTH:+SHA1:+kRS
  18. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  19. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 默认 TLSv1 TLSv1.1 TLSv1.2;
  20. # 设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。
  21. ssl_prefer_server_ciphers on;
  22. # 开启自定义错误跳转页面, 这样服务器返回错误就被nginx捕获跳转 ...
  23. # 可以根据响应状态码的值进行拦截错误处理,与error_page 指令相互结合。
  24. # 否则是不捕获上游错误状态码的
  25. #proxy_intercept_errors on;
  26. # 自定义统一 404 页面
  27. error_page 404 /404.html;
  28. location /404.html {
  29. root /usr/local/nginx/html;
  30. }
  31. # 捕获异常状态码,相应指定静态页面
  32. error_page 500 502 503 504 /error.html;
  33. location = /error.html {
  34. root /usr/local/nginx/html;
  35. }
  36. # 开启指定文件的跨域访问
  37. location ~* \.(ttf|ttc|otf|eot|woff|font.css)$ {
  38. add_header Access-Control-Allow-Origin *;
  39. }
  40. # 反向代理 -> 详情下面
  41. location / {
  42. proxy_pass http://127.0.0.1:666;
  43. #proxy_redirect off;
  44. proxy_redirect ~^http://127.0.0.1:666(.*) https://www.domain.com$1;
  45. proxy_set_header Host $proxy_host;
  46. proxy_set_header X-Real-IP $remote_addr;
  47. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  48. proxy_http_version 1.1;
  49. proxy_set_header Upgrade $http_upgrade;
  50. proxy_set_header Connection "upgrade";
  51. }
  52. # 把指定的 uri 重定向到新的网址,并且携带访问的网址 uri
  53. location /domainuri {
  54. rewrite ^.* https://www.domain.com/xxx/Body?requrl=$request_uri? permanent;
  55. }
  56. # 反向代理到 oss
  57. location /oss/ {
  58. proxy_pass http://domain.oss-cn-index.aliyuncs.com/;
  59. proxy_redirect off;
  60. proxy_set_header Host www.domain.com;
  61. proxy_set_header X-Real-IP $remote_addr;
  62. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  63. proxy_http_version 1.1;
  64. proxy_set_header Upgrade $http_upgrade;
  65. proxy_set_header Connection "upgrade";
  66. add_header Access-Control-Allow-Origin *;
  67. }
  68. }

简单版

  1. server {
  2. listen 80;
  3. server_name domain.com www.domain.com;
  4. location / {
  5. proxy_pass http://127.0.0.1:666;
  6. #proxy_redirect off;
  7. proxy_redirect ~^http://127.0.0.1:666(.*) https://www.domain.com$1;
  8. proxy_set_header Host $proxy_host;
  9. proxy_set_header X-Real-IP $remote_addr;
  10. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  11. proxy_http_version 1.1;
  12. proxy_set_header Upgrade $http_upgrade;
  13. proxy_set_header Connection "upgrade";
  14. }
  15. }

测试是否正常,重加载 nginx 配置文件

  1. ./nginx -s reload

Nginx 反向代理

在配置文件 server 节点下配置

  1. location / {
  2. #
  3. proxy_pass http://127.0.0.1:666;
  4. #proxy_redirect off;
  5. proxy_redirect ~^http://127.0.0.1:666(.*) https://www.domain.com$1;
  6. proxy_set_header Host $proxy_host;
  7. proxy_set_header X-Real-IP $remote_addr;
  8. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  9. # 如果是反向代理的话,对 WebSocket 进行支持。通过Http升级机制将Http升级为WebSocket
  10. proxy_http_version 1.1;
  11. proxy_set_header Upgrade $http_upgrade;
  12. proxy_set_header Connection "upgrade";
  13. # 其它不常用配置项
  14. #proxy_connect_timeout 30;
  15. #proxy_read_timeout 60;
  16. #proxy_send_timeout 60; # 两次write操作期间没有收到新的数据,就关闭链接
  17. #proxy_buffer_size 32k;
  18. #proxy_buffering on;
  19. #proxy_buffers 4 128k;
  20. #proxy_busy_buffers_size 256k;
  21. #proxy_max_temp_file_size 256k;
  22. }

Nginx 负载均衡

在配置文件 server 节点下配置

  1. http {
  2. upstream myproject {
  3. server 127.0.0.1:8000 weight=3;
  4. server 127.0.0.1:8001;
  5. server 127.0.0.1:8002;
  6. #down表示当前的服务器暂时不可用,不参与负载均衡
  7. server www.aaa.com down;
  8. #max_conns=100表示此服务器的最大连接数为100
  9. server www.bbb.com:1234 max_conns=100;
  10. #weight表示权重,权重越大,被分配到的次数就越多
  11. server www.ccc.com weight=3;
  12. #backup表示这是预留的备份服务器,只在其他的节点都不可用的时候才启用
  13. server www.ddd.com backup;
  14. #max_fails在连续超过3次失败后,30秒内不要再分发请求到此服务器,默认fail_timeout为10秒
  15. server x.x.x.x:8080 max_fails=3 fail_timeout=30s;
  16. }
  17. server {
  18. listen 80;
  19. server_name www.domain.com;
  20. location / {
  21. proxy_pass http://myproject;
  22. # ......
  23. }
  24. }
  25. }

weight 参数用于指定轮询几率,weight的默认值为 1,weight 的数值与访问比率成正比,权重越高分配到需要处理的请求越多。 此策略可以与least_conn和ip_hash结合使用主要用于后端服务器性能不均

HTTP2 配置

nginx 开启 http2非常简单,在nginx.conf中只需在原来监听的端口后面加上http2就可以了,前提是你的nginx版本不能低于1.95,并且已经开启了https

  1. listen 443 ssl http2;

在network中通过protocol可以查看到当前的资源是通过哪个版本的http协议传输的 h2代表http2 Nginx 安装配置 - 图4

  1. server {
  2. listen 443 ssl;
  3. server_name domain.com www.domain.com;
  4. ssl_certificate /usr/local/nginx/SSL/domain.com/domain.com.pem;
  5. ssl_certificate_key /usr/local/nginx/SSL/domain.com/domain.com.key;
  6. ssl_session_timeout 5m;
  7. # 默认 HIGH:!RC4:!3DES:!aDSS:!aNULL:!kPSK:!kSRP:!MD5:@STRENGTH:+SHA1:+kRS
  8. ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
  9. ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 默认 TLSv1 TLSv1.1 TLSv1.2;
  10. # 设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。
  11. ssl_prefer_server_ciphers on;
  12. # 开启自定义错误跳转页面, 这样服务器返回错误就被nginx捕获跳转 ...
  13. # 可以根据响应状态码的值进行拦截错误处理,与error_page 指令相互结合。
  14. # 否则是不捕获上游错误状态码的
  15. #proxy_intercept_errors on;
  16. # 自定义统一 404 页面
  17. error_page 404 /404.html;
  18. location /404.html {
  19. root /usr/local/nginx/html;
  20. }
  21. # 捕获异常状态码,相应指定静态页面
  22. error_page 500 502 503 504 /error.html;
  23. location = /error.html {
  24. root /usr/local/nginx/html;
  25. }
  26. # 反向代理 -> 详情下面
  27. location / {
  28. proxy_pass http://127.0.0.1:666;
  29. #proxy_redirect off;
  30. proxy_redirect ~^http://127.0.0.1:7011(.*) https://www.domain.com$1;
  31. proxy_set_header Host $proxy_host;
  32. proxy_set_header X-Real-IP $remote_addr;
  33. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  34. proxy_http_version 1.1;
  35. proxy_set_header Upgrade $http_upgrade;
  36. proxy_set_header Connection "upgrade";
  37. }
  38. }