1. 环境搭建

nginx - 图1

1.1 编译

  1. # 下载代码
  2. git clone git@192.168.128.110:chromium/nginx.git nginx-1.22.0
  3. git clone https://192.168.128.110/chromium/nginx.git nginx-1.22.0
  4. # 生成makefile
  5. cd nginx-1.22.0
  6. ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx \
  7. --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf \
  8. --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log \
  9. --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock \
  10. --http-client-body-temp-path=/var/cache/nginx/client_temp \
  11. --http-proxy-temp-path=/var/cache/nginx/proxy_temp \
  12. --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
  13. --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
  14. --http-scgi-temp-path=/var/cache/nginx/scgi_temp \
  15. --user=nginx --group=nginx --with-compat --with-file-aio --with-threads \
  16. --with-http_addition_module --with-http_auth_request_module \
  17. --with-http_dav_module --with-http_flv_module --with-http_gunzip_module \
  18. --with-http_gzip_static_module --with-http_mp4_module \
  19. --with-http_random_index_module --with-http_realip_module \
  20. --with-http_secure_link_module --with-http_slice_module \
  21. --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module \
  22. --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream \
  23. --with-stream_realip_module --with-stream_ssl_module \
  24. --with-stream_ssl_preread_module \
  25. --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' \
  26. --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
  27. # 输出
  28. Configuration summary
  29. + using threads
  30. + using system PCRE2 library
  31. + using system OpenSSL library
  32. + using system zlib library
  33. nginx path prefix: "/etc/nginx"
  34. nginx binary file: "/usr/sbin/nginx"
  35. nginx modules path: "/usr/lib64/nginx/modules"
  36. nginx configuration prefix: "/etc/nginx"
  37. nginx configuration file: "/etc/nginx/nginx.conf"
  38. nginx pid file: "/var/run/nginx.pid"
  39. nginx error log file: "/var/log/nginx/error.log"
  40. nginx http access log file: "/var/log/nginx/access.log"
  41. nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  42. nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  43. nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  44. nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  45. nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"
  46. # 编译
  47. make -j4
  48. # 安装
  49. make install
  50. # 创建nginx临时目录
  51. mkdir -p /var/cache/nginx
  52. # debug版本
  53. 编译选项:--with-debug
  54. 配置文件
  55. master_process off;
  56. daemon off;

1.2 配置

upstream设置:

  • 轮询【默认】:每次请求都会切换server,同一个session会在多个服务器上切换
  • ip_hash:hash算法会导致同一网段都在一个server上,不均衡

负载均衡配置:

  1. #user nobody;
  2. worker_processes 4;
  3. events {
  4. # 最大并发数
  5. worker_connections 1024;
  6. }
  7. http {
  8. # 将nginx版本号隐藏掉
  9. server_tokens off;
  10. # 待选服务器列表
  11. upstream myproject {
  12. # ip_hash指令,将同一用户引入同一服务器。
  13. ip_hash;
  14. server 10.0.32.159:80;
  15. server 10.0.32.169:80;
  16. }
  17. server{
  18. # 监听端口
  19. listen 80;
  20. # 根目录下
  21. location / {
  22. # 选择哪个服务器列表
  23. proxy_pass http://myproject;
  24. }
  25. }
  26. }

1.3 启动

  1. # 启动
  2. nginx
  3. # 停止
  4. nginx -s stop
  5. # 重启
  6. nginx -s reload

2. 内部变量

名称 说明
$arg_name 请求中的name参数
$args 请求中的参数
$binary_remote_addr 远程地址的二进制表示
$body_bytes_sent 已发送的消息体字节数
$content_length HTTP请求信息里的”Content-Length”
$content_type 请求信息里的”Content-Type”
$document_root 针对当前请求的根路径设置值
$document_uri 与$uri相同; 比如 /test2/test.php
$host 请求信息中的”Host”,如果请求中没有Host行,则等于设置的服务器名
$hostname 机器名使用 gethostname系统调用的值
$http_cookie cookie 信息
$http_referer 引用地址
$http_user_agent 客户端代理信息
$http_via 最后一个访问服务器的Ip地址。
$http_x_forwarded_for 相当于网络访问路径
$is_args 如果请求行带有参数,返回“?”,否则返回空字符串
$limit_rate 对连接速率的限制
$nginx_version 当前运行的nginx版本号
$pid worker进程的PID
$query_string 与$args相同
$realpath_root 按root指令或alias指令算出的当前请求的绝对路径。其中的符号链接都会解析成真是文件路径
$remote_addr 客户端IP地址
$remote_port 客户端端口号
$remote_user 客户端用户名,认证用
$request 用户请求
$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义
$request_body_file 客户端请求主体信息的临时文件名
$request_completion 如果请求成功,设为”OK”;如果请求未完成或者不是一系列请求中最后一部分则设为空
$request_filename 当前请求的文件路径名,比如/opt/nginx/www/test.php
$request_method 请求的方法,比如”GET”、”POST”等
$request_uri 请求的URI,带参数
$scheme 所用的协议,比如http或者是https
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$server_name 请求到达的服务器名
$server_port 请求到达的服务器端口号
$server_protocol 请求的协议版本,”HTTP/1.0”或”HTTP/1.1”
$uri 请求的URI,可能和最初的值有不同,比如经过重定向之类的

3. location配置

匹配符 说明
= 严格匹配。如果这个查询匹配,那么将停止搜索并立即处理此请求
~ 区分大小写匹配(可用正则表达式)
!~ 区分大小写不匹配
~* 不区分大小写匹配(可用正则表达式)
!~* 不区分大小写不匹配
^~ 即表示只匹配普通字符(空格)。使用前缀匹配,^表示“非”,即不查询正则表达式。如果匹配成功,则不再匹配其他location
@ 指定一个命名的location,一般只用于内部重定向请求。例如 error_page, try_files

参考文档