1. 环境搭建
1.1 编译
# 下载代码git clone git@192.168.128.110:chromium/nginx.git nginx-1.22.0或git clone https://192.168.128.110/chromium/nginx.git nginx-1.22.0# 生成makefilecd nginx-1.22.0./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx \--modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/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-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-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' \--with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'# 输出Configuration summary+ using threads+ using system PCRE2 library+ using system OpenSSL library+ using system zlib librarynginx path prefix: "/etc/nginx"nginx binary file: "/usr/sbin/nginx"nginx modules path: "/usr/lib64/nginx/modules"nginx configuration prefix: "/etc/nginx"nginx configuration file: "/etc/nginx/nginx.conf"nginx pid file: "/var/run/nginx.pid"nginx error log file: "/var/log/nginx/error.log"nginx http access log file: "/var/log/nginx/access.log"nginx http client request body temporary files: "/var/cache/nginx/client_temp"nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"# 编译make -j4# 安装make install# 创建nginx临时目录mkdir -p /var/cache/nginx# debug版本编译选项:--with-debug配置文件master_process off;daemon off;
1.2 配置
upstream设置:
- 轮询【默认】:每次请求都会切换server,同一个session会在多个服务器上切换
- ip_hash:hash算法会导致同一网段都在一个server上,不均衡
负载均衡配置:
#user nobody;worker_processes 4;events {# 最大并发数worker_connections 1024;}http {# 将nginx版本号隐藏掉server_tokens off;# 待选服务器列表upstream myproject {# ip_hash指令,将同一用户引入同一服务器。ip_hash;server 10.0.32.159:80;server 10.0.32.169:80;}server{# 监听端口listen 80;# 根目录下location / {# 选择哪个服务器列表proxy_pass http://myproject;}}}
1.3 启动
# 启动nginx# 停止nginx -s stop# 重启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 |
