- Nginx 源代码分析 https://lobin.iteye.com/admin/blogs/2518134
- Nginx 源代码分析 - 模块 https://lobin.iteye.com/admin/blogs/2518135
- Nginx 源代码分析 - main https://lobin.iteye.com/admin/blogs/2518066
- Nginx 源代码分析 - struct ngx_cycle_s https://lobin.iteye.com/admin/blogs/2518065
- Nginx 源代码分析 - struct ngx_pool_s https://lobin.iteye.com/admin/blogs/2518067
- Nginx 源代码分析 - 进程 https://lobin.iteye.com/admin/blogs/2518132
- Nginx 源代码分析 - 主(master)进程 https://lobin.iteye.com/admin/blogs/2518131
- Nginx 源代码分析 - 工作进程 https://lobin.iteye.com/admin/blogs/2518130
- Nginx 源代码分析 - cache进程 https://lobin.iteye.com/admin/blogs/2518133
安装
# ./configure --prefix=/usr/local/nginx-1.19.3
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
# yum install -y pcre pcre-devel
# ./configure --prefix=/usr/local/nginx-1.19.3
安装SSL模块
# ./configure --with-http_ssl_module --prefix=/usr/local/nginx-1.19.3
确认安装了OpenSSL
# openssl version
OpenSSL 1.0.1e-fips 11 Feb 2013
# make
# make install
│ ├── fastcgi.conf
│ ├── fastcgi.conf.default
│ ├── fastcgi_params
│ ├── fastcgi_params.default
│ ├── koi-utf
│ ├── koi-win
│ ├── mime.types
│ ├── mime.types.default
│ ├── nginx.conf
│ ├── nginx.conf.default
│ ├── scgi_params
│ ├── scgi_params.default
│ ├── uwsgi_params
│ ├── uwsgi_params.default
│ └── win-utf
├── html
│ ├── 50x.html
│ └── index.html
├── logs
└── sbin
└── nginx
配置
配置文件
nginx.conf
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
负载均衡配置
upstream
Default: —
Context: http
默认采用基于权重的轮训负载均衡策略。
Default: —
Context: upstream
weight=number
sets the weight of the server, by default, 1.
max_conns=number
limits the maximum number of simultaneous active connections to the proxied server (1.11.5). Default value is zero, meaning there is no limit. If the server group does not reside in the shared memory, the limitation works per each worker process.
If idle keepalive connections, multiple workers, and the shared memory are enabled, the total number of active and idle connections to the proxied server may exceed the max_conns value.
Since version 1.5.9 and prior to version 1.11.5, this parameter was available as part of our commercial subscription.
max_fails=number
sets the number of unsuccessful attempts to communicate with the server that should happen in the duration set by the fail_timeout parameter to consider the server unavailable for a duration also set by the fail_timeout parameter. By default, the number of unsuccessful attempts is set to 1. The zero value disables the accounting of attempts. What is considered an unsuccessful attempt is defined by the proxy_next_upstream, fastcgi_next_upstream, uwsgi_next_upstream, scgi_next_upstream, memcached_next_upstream, and grpc_next_upstream directives.
fail_timeout=time
sets
1. the time during which the specified number of unsuccessful attempts to communicate with the server should happen to consider the server unavailable;
2. and the period of time the server will be considered unavailable.
By default, the parameter is set to 10 seconds.
backup
marks the server as a backup server. It will be passed requests when the primary servers are unavailable.
The parameter cannot be used along with the hash, ip_hash, and random load balancing methods.
down
marks the server as permanently unavailable.
resolve
monitors changes of the IP addresses that correspond to a domain name of the server, and automatically modifies the upstream configuration without the need of restarting nginx (1.5.12). The server group must reside in the shared memory.
In order for this parameter to work, the resolver directive must be specified in the http block or in the corresponding upstream block.
route=string
sets the server route name.
service=name
enables resolving of DNS SRV records and sets the service name (1.9.13). In order for this parameter to work, it is necessary to specify the resolve parameter for the server and specify a hostname without a port number.
If the service name does not contain a dot (“.”), then the RFC-compliant name is constructed and the TCP protocol is added to the service prefix. For example, to look up the _http._tcp.backend.example.com SRV record, it is necessary to specify the directive:
server backend.example.com service=http resolve;
If the service name contains one or more dots, then the name is constructed by joining the service prefix and the server name. For example, to look up the _http._tcp.backend.example.com and server1.backend.example.com SRV records, it is necessary to specify the directives:
server backend.example.com service=_http._tcp resolve;
server example.com service=server1.backend resolve;
Highest-priority SRV records (records with the same lowest-number priority value) are resolved as primary servers, the rest of SRV records are resolved as backup servers. If the backup parameter is specified for the server, high-priority SRV records are resolved as backup servers, the rest of SRV records are ignored.
slow_start=time
sets the time during which the server will recover its weight from zero to a nominal value, when unhealthy server becomes healthy, or when the server becomes available after a period of time it was considered unavailable. Default value is zero, i.e. slow start is disabled.
The parameter cannot be used along with the hash, ip_hash, and random load balancing methods.
drain
puts the server into the “draining” mode (1.13.6). In this mode, only requests bound to the server will be proxied to it.
Prior to version 1.13.6, the parameter could be changed only with the API module.
If there is only a single server in a group, max_fails, fail_timeout and slow_start parameters are ignored, and such a server will never be considered unavailable.
upstream backend { server 192.168.0.102:8081; server 192.168.0.102:8082; server 192.168.0.102:8083; } location / { proxy_pass http://backend; }
负载均衡策略
Default: —
Context: upstream
This directive appeared in version 1.7.2.
Default: —
Context: upstream
Default: —
Context: upstream
This directive appeared in versions 1.3.1 and 1.2.2.
Default: —
Context: upstream
This directive appeared in version 1.7.10.
Default: —
Context: upstream
This directive appeared in version 1.15.1.
可选参数
method
默认为least_conn。社区版本只支持least_conn,商业版本还支持least_time。
upstream order.com { random; server 192.168.0.102:8081; server 192.168.0.102:8082; server 192.168.0.102:8083; } location / { proxy_pass http://order.com; }
upstream m.order.com { random two; server 192.168.0.102:8081; server 192.168.0.102:8082; server 192.168.0.102:8083; } location / { proxy_pass http://m.order.com; }
等同于
upstream m.order.com { random two least_conn; server 192.168.0.102:8081; server 192.168.0.102:8082; server 192.168.0.102:8083; } location / { proxy_pass http://m.order.com; }
访问日志配置
默认情况下,nginx将日志记录到logs/access.log中,记录格式:combined。默认访问日志配置等同于如下配置:
access_log logs/access.log combined;
自定义访问日志文件
access_log /var/logs/nginx/access.log
自定义访问日志格式
通过log_format自定义访问日志格式
如默认的访问日志格式combined的格式:
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent"';
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
[root@localhost nginx-1.19.3]# tail -f logs/access.log
192.168.0.109 - - [08/Nov/2020:00:32:22 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
192.168.0.109 - - [08/Nov/2020:00:32:23 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
192.168.0.109 - - [08/Nov/2020:00:32:23 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36" "-"
# ./nginx -V
nginx version: nginx/1.19.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
configure arguments: --prefix=/usr/local/nginx-1.19.3
安装了SSL模块
# ./nginx -V
nginx version: nginx/1.19.3
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC)
built with OpenSSL 1.0.1e-fips 11 Feb 2013
TLS SNI support enabled
configure arguments: --with-http_ssl_module --prefix=/usr/local/nginx-1.19.3
# nginx
# ps -ef | grep nginx
root 6290 1 1 12:17 ? 00:00:00 nginx: master process nginx
nobody 6291 6290 0 12:17 ? 00:00:00 nginx: worker process
# tail -f logs/access.log
192.168.0.109 - - [07/Nov/2020:23:50:55 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
192.168.0.109 - - [07/Nov/2020:23:50:55 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
192.168.0.109 - - [07/Nov/2020:23:50:55 +0800] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36"
# tail -f logs/error.log
2020/10/18 16:45:11 [error] 21936#0: *1 open() "/usr/local/nginx-1.19.3/html/favicon.ico" failed (2: No such file or directory), client: 192.168.0.109, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "192.168.0.102", referrer: "http://192.168.0.102/"
配置https模块
# openssl genrsa -idea -out nginx.key 1024
Generating RSA private key, 1024 bit long modulus
..................................++++++
.....++++++
e is 65537 (0x10001)
Enter pass phrase for nginx.key:
Verifying - Enter pass phrase for nginx.key:
密码为123456
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: IDEA-CBC,DBD65C6553A3BBBA
vsfcFOj1ke+ub9qm7IVvWFIdGjb0vrJ9Rscc1Cr4vJoQRjkqAVuKHVPJaQI7K0uJ
sRoWl9C6IIj3jRGrmeJs0lNHVGfBpFqL2O/vImsNPVW/IIrG79yI4UJrEgTIlzwu
ara6w5irL46zk8ECLHiGfeTbQaIoNjemNoVPkx2i3yW6tPQi4+XVbawiSZqWXrw5
k0lgqEMtvJhd+8F7P/WTVVi+1AHYh9qZIus3C4RLOoR6yY5z1H4x/PQcpESDnNei
YQ5PdMya/Rg+Rd99nTwEZ+XhCqWVNE2/qhDju5/T6RmzPqSGvAktFJrRLzgPRlr7
LNmtZ50L9Eo2mO0Quunv7Rca0uKmZtot7dV3YWyVCLRyIaXBy11cXB+rgzNb2/7N
xpYOepTbckC/YY3ZXm6IkSdQlqCsmUwGinFztFNByhKPAk3xH5EfBeEfafE/0uO9
49Xtnk83ROTaayOUqV8eKAHOEz6jsPGQ8ooziBmYi5Lpv9HnevS3QHmwwnehMTIY
iZ2dYH94pHwDTlTyyX0Zk/CUNz/I9ukyUZ6NNPxmcNtO6PvyUJBw+ubQ4WudZlV/
rQFJD5PpB8D9cY7hZ4tHMQyFTqvoIZIIDF+e5s2HGXkc9vaNj7D2dhXgJ0PLdLbs
zFiuzUSyB0WPh57rcvV3Pvee/eNIUmsmYzXpzri3QCdU5o/cVUS9jtohrsFHeR+i
KOfmFISExkL/Bh+uuNcFxXFiECVTWUmcmjW3XjLDr4sPyDWTXpj9q9sNyij8qDHp
7j6yq3fz9+MvdouU6m4dO1RwzVQz4n1T5Z9ar3Spi73BclpYs/IBPg==
-----END RSA PRIVATE KEY-----
# openssl req -new -key nginx.key -out nginx.csr
Enter pass phrase for nginx.key:
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:cn
State or Province Name (full name) []:sh
Locality Name (eg, city) [Default City]:sh
Organization Name (eg, company) [Default Company Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (eg, your name or your server's hostname) []:
Email Address []:
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:123456
An optional company name []:
-----BEGIN CERTIFICATE REQUEST-----
MIIBnDCCAQUCAQAwRTELMAkGA1UEBhMCY24xCzAJBgNVBAgMAnNoMQswCQYDVQQH
DAJzaDEcMBoGA1UECgwTRGVmYXVsdCBDb21wYW55IEx0ZDCBnzANBgkqhkiG9w0B
AQEFAAOBjQAwgYkCgYEAxm8nHANqDt6lpsWxBslUgNENaqmEh9lJ9lqHg1tput8b
zKAd8DjPbMGRO2uMobcv1iFp5jxqBx6RnazU8EoHriKNoSqqKRMJh/lzRxj9UUG1
MV6Kkzc6z0C9XdZ7cymztO2rTaHOdfRhdHp8nmdUkeLonotqad8LStJMzsiXvGMC
AwEAAaAXMBUGCSqGSIb3DQEJBzEIDAYxMjM0NTYwDQYJKoZIhvcNAQEFBQADgYEA
C5uQ7ni3zYJAN1Owis4npoSTUwbnk2NtX1kOIe/kJV0MwPxr249E7D7bGojwgJWK
5yclYia7aH5na/aZyTFI8Y+cd/M48c0+Hef5rc/8jjij3Wh/lD183bW8OJvc2oVf
ZTH/EUjAvQlyM1b26x9UFhBk1zILWcR2HwH5DfmynuQ=
-----END CERTIFICATE REQUEST-----
# openssl x509 -req -days 3650 -in nginx.csr -signkey nginx.key -out nginx.crt
Signature ok
subject=/C=cn/ST=sh/L=sh/O=Default Company Ltd
Getting Private key
Enter pass phrase for nginx.key:
-----BEGIN CERTIFICATE-----
MIICATCCAWoCCQCulbkjPHscmjANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJj
bjELMAkGA1UECAwCc2gxCzAJBgNVBAcMAnNoMRwwGgYDVQQKDBNEZWZhdWx0IENv
bXBhbnkgTHRkMB4XDTIwMTAxODA4NDEzNFoXDTMwMTAxNjA4NDEzNFowRTELMAkG
A1UEBhMCY24xCzAJBgNVBAgMAnNoMQswCQYDVQQHDAJzaDEcMBoGA1UECgwTRGVm
YXVsdCBDb21wYW55IEx0ZDCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEAxm8n
HANqDt6lpsWxBslUgNENaqmEh9lJ9lqHg1tput8bzKAd8DjPbMGRO2uMobcv1iFp
5jxqBx6RnazU8EoHriKNoSqqKRMJh/lzRxj9UUG1MV6Kkzc6z0C9XdZ7cymztO2r
TaHOdfRhdHp8nmdUkeLonotqad8LStJMzsiXvGMCAwEAATANBgkqhkiG9w0BAQUF
AAOBgQBMb0oFBPN3GM5BpyMW/71lXjHkIESiAW0gQ4JWmi68pU3GVSNyU64oV8Lv
VuW6nqhoMiB8oSiFeNhKN3DOozwFnqJ4tHwkyyfwh5XveZELsQ9zepw4t51+1ayz
BMCx3kpH7BFwPHW1ZROVGVEPn6L1l0btPvaMJkzmqgWXn6Skuw==
-----END CERTIFICATE-----
# ll
total 5580
-rwxr-xr-x. 1 root root 5699297 Oct 18 16:33 nginx
-rw-r--r--. 1 root root 757 Oct 18 16:41 nginx.crt
-rw-r--r--. 1 root root 635 Oct 18 16:40 nginx.csr
-rw-r--r--. 1 root root 959 Oct 18 16:36 nginx.key
#
server {
listen 443 ssl;
server_name localhost;
ssl_certificate ../cert/nginx.crt;
ssl_certificate_key ../cert/nginx.key;
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 5m;
location / {
root html;
index index.html index.htm;
}
}
# ./nginx -t -c /usr/local/nginx-1.19.3/conf/nginx.conf
Enter PEM pass phrase:
nginx: the configuration file /usr/local/nginx-1.19.3/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx-1.19.3/conf/nginx.conf test is successful
这里需要输入刚才的密码
# ./nginx -s reload
Enter PEM pass phrase:
这里需要输入刚才的密码
重启
# ./nginx
Enter PEM pass phrase:
这里需要输入刚才的密码
开启443端口
# cat /etc/sysconfig/iptables
-A INPUT -p tcp --dport 443 -j ACCEPT
# service iptables restart
curl: (60) Peer certificate cannot be authenticated with known CA certificates
More details here: http://curl.haxx.se/docs/sslcerts.html
curl performs SSL certificate verification by default, using a "bundle"
of Certificate Authority (CA) public keys (CA certs). If the default
bundle file isn't adequate, you can specify an alternate file
using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
the bundle, the certificate verification probably failed due to a
problem with the certificate (it might be expired, or the name might
not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
the -k (or --insecure) option.
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
nginx rewrite
rewrite |
— |
server , location , if
|
An optional flag parameter can be one of:
last
stops processing the current set of ngx_http_rewrite_module directives and starts a search for a new location matching the changed URI;
break
stops processing the current set of ngx_http_rewrite_module directives as with the breakdirective;
redirect
returns a temporary redirect with the 302 code; used if a replacement string does not start with “http://” or “https://”;
permanent
returns a permanent redirect with the 301 code.
The full redirect URL is formed according to the request scheme ($scheme) and theserver_name_in_redirect and port_in_redirect directives.
Example:
server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403; ... }
location /download/ { rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403; }
rewrite ^/users/(.*)$ /show?user=$1? last;
源代码
ngx_auto_config.h
配置头文件,源代码中没有这个文件。这个文件在./configure的时候自动生成。
#define NGX_CONFIGURE " --prefix=/usr/local/nginx-1.19.3-dev" #ifndef NGX_COMPILER #define NGX_COMPILER "gcc 4.4.7 20120313 (Red Hat 4.4.7-23) (GCC) " #endif #ifndef NGX_HAVE_GCC_ATOMIC #define NGX_HAVE_GCC_ATOMIC 1 #endif #ifndef NGX_HAVE_C99_VARIADIC_MACROS #define NGX_HAVE_C99_VARIADIC_MACROS 1 #endif #ifndef NGX_HAVE_GCC_VARIADIC_MACROS #define NGX_HAVE_GCC_VARIADIC_MACROS 1 #endif #ifndef NGX_HAVE_GCC_BSWAP64 #define NGX_HAVE_GCC_BSWAP64 1 #endif #ifndef NGX_HAVE_EPOLL #define NGX_HAVE_EPOLL 1 #endif #ifndef NGX_HAVE_CLEAR_EVENT #define NGX_HAVE_CLEAR_EVENT 1 #endif #ifndef NGX_HAVE_EPOLLRDHUP #define NGX_HAVE_EPOLLRDHUP 1 #endif #ifndef NGX_HAVE_SENDFILE #define NGX_HAVE_SENDFILE 1 #endif #ifndef NGX_HAVE_SENDFILE64 #define NGX_HAVE_SENDFILE64 1 #endif #ifndef NGX_HAVE_PR_SET_DUMPABLE #define NGX_HAVE_PR_SET_DUMPABLE 1 #endif #ifndef NGX_HAVE_PR_SET_KEEPCAPS #define NGX_HAVE_PR_SET_KEEPCAPS 1 #endif #ifndef NGX_HAVE_CAPABILITIES #define NGX_HAVE_CAPABILITIES 1 #endif #ifndef NGX_HAVE_GNU_CRYPT_R #define NGX_HAVE_GNU_CRYPT_R 1 #endif #ifndef NGX_HAVE_NONALIGNED #define NGX_HAVE_NONALIGNED 1 #endif #ifndef NGX_CPU_CACHE_LINE #define NGX_CPU_CACHE_LINE 32 #endif #define NGX_KQUEUE_UDATA_T (void *) #ifndef NGX_HAVE_POSIX_FADVISE #define NGX_HAVE_POSIX_FADVISE 1 #endif #ifndef NGX_HAVE_O_DIRECT #define NGX_HAVE_O_DIRECT 1 #endif #ifndef NGX_HAVE_ALIGNED_DIRECTIO #define NGX_HAVE_ALIGNED_DIRECTIO 1 #endif #ifndef NGX_HAVE_STATFS #define NGX_HAVE_STATFS 1 #endif #ifndef NGX_HAVE_STATVFS #define NGX_HAVE_STATVFS 1 #endif #ifndef NGX_HAVE_DLOPEN #define NGX_HAVE_DLOPEN 1 #endif #ifndef NGX_HAVE_SCHED_YIELD #define NGX_HAVE_SCHED_YIELD 1 #endif #ifndef NGX_HAVE_SCHED_SETAFFINITY #define NGX_HAVE_SCHED_SETAFFINITY 1 #endif #ifndef NGX_HAVE_REUSEPORT #define NGX_HAVE_REUSEPORT 1 #endif #ifndef NGX_HAVE_TRANSPARENT_PROXY #define NGX_HAVE_TRANSPARENT_PROXY 1 #endif #ifndef NGX_HAVE_IP_PKTINFO #define NGX_HAVE_IP_PKTINFO 1 #endif #ifndef NGX_HAVE_IPV6_RECVPKTINFO #define NGX_HAVE_IPV6_RECVPKTINFO 1 #endif #ifndef NGX_HAVE_DEFERRED_ACCEPT #define NGX_HAVE_DEFERRED_ACCEPT 1 #endif #ifndef NGX_HAVE_KEEPALIVE_TUNABLE #define NGX_HAVE_KEEPALIVE_TUNABLE 1 #endif #ifndef NGX_HAVE_TCP_INFO #define NGX_HAVE_TCP_INFO 1 #endif #ifndef NGX_HAVE_ACCEPT4 #define NGX_HAVE_ACCEPT4 1 #endif #ifndef NGX_HAVE_EVENTFD #define NGX_HAVE_EVENTFD 1 #endif #ifndef NGX_HAVE_SYS_EVENTFD_H #define NGX_HAVE_SYS_EVENTFD_H 1 #endif #ifndef NGX_HAVE_UNIX_DOMAIN #define NGX_HAVE_UNIX_DOMAIN 1 #endif #ifndef NGX_PTR_SIZE #define NGX_PTR_SIZE 4 #endif #ifndef NGX_SIG_ATOMIC_T_SIZE #define NGX_SIG_ATOMIC_T_SIZE 4 #endif #ifndef NGX_HAVE_LITTLE_ENDIAN #define NGX_HAVE_LITTLE_ENDIAN 1 #endif #ifndef NGX_MAX_SIZE_T_VALUE #define NGX_MAX_SIZE_T_VALUE 2147483647 #endif #ifndef NGX_SIZE_T_LEN #define NGX_SIZE_T_LEN (sizeof("-2147483648") - 1) #endif #ifndef NGX_MAX_OFF_T_VALUE #define NGX_MAX_OFF_T_VALUE 9223372036854775807LL #endif #ifndef NGX_OFF_T_LEN #define NGX_OFF_T_LEN (sizeof("-9223372036854775808") - 1) #endif #ifndef NGX_TIME_T_SIZE #define NGX_TIME_T_SIZE 4 #endif #ifndef NGX_TIME_T_LEN #define NGX_TIME_T_LEN (sizeof("-2147483648") - 1) #endif #ifndef NGX_MAX_TIME_T_VALUE #define NGX_MAX_TIME_T_VALUE 2147483647 #endif #ifndef NGX_HAVE_INET6 #define NGX_HAVE_INET6 1 #endif #ifndef NGX_HAVE_PREAD #define NGX_HAVE_PREAD 1 #endif #ifndef NGX_HAVE_PWRITE #define NGX_HAVE_PWRITE 1 #endif #ifndef NGX_HAVE_PWRITEV #define NGX_HAVE_PWRITEV 1 #endif #ifndef NGX_SYS_NERR #define NGX_SYS_NERR 135 #endif #ifndef NGX_HAVE_LOCALTIME_R #define NGX_HAVE_LOCALTIME_R 1 #endif #ifndef NGX_HAVE_CLOCK_MONOTONIC #define NGX_HAVE_CLOCK_MONOTONIC 1 #endif #ifndef NGX_HAVE_POSIX_MEMALIGN #define NGX_HAVE_POSIX_MEMALIGN 1 #endif #ifndef NGX_HAVE_MEMALIGN #define NGX_HAVE_MEMALIGN 1 #endif #ifndef NGX_HAVE_MAP_ANON #define NGX_HAVE_MAP_ANON 1 #endif #ifndef NGX_HAVE_MAP_DEVZERO #define NGX_HAVE_MAP_DEVZERO 1 #endif #ifndef NGX_HAVE_SYSVSHM #define NGX_HAVE_SYSVSHM 1 #endif #ifndef NGX_HAVE_POSIX_SEM #define NGX_HAVE_POSIX_SEM 1 #endif #ifndef NGX_HAVE_MSGHDR_MSG_CONTROL #define NGX_HAVE_MSGHDR_MSG_CONTROL 1 #endif #ifndef NGX_HAVE_FIONBIO #define NGX_HAVE_FIONBIO 1 #endif #ifndef NGX_HAVE_FIONREAD #define NGX_HAVE_FIONREAD 1 #endif #ifndef NGX_HAVE_GMTOFF #define NGX_HAVE_GMTOFF 1 #endif #ifndef NGX_HAVE_D_TYPE #define NGX_HAVE_D_TYPE 1 #endif #ifndef NGX_HAVE_SC_NPROCESSORS_ONLN #define NGX_HAVE_SC_NPROCESSORS_ONLN 1 #endif #ifndef NGX_HAVE_LEVEL1_DCACHE_LINESIZE #define NGX_HAVE_LEVEL1_DCACHE_LINESIZE 1 #endif #ifndef NGX_HAVE_OPENAT #define NGX_HAVE_OPENAT 1 #endif #ifndef NGX_HAVE_GETADDRINFO #define NGX_HAVE_GETADDRINFO 1 #endif #ifndef NGX_HTTP_CACHE #define NGX_HTTP_CACHE 1 #endif #ifndef NGX_HTTP_GZIP #define NGX_HTTP_GZIP 1 #endif #ifndef NGX_HTTP_SSI #define NGX_HTTP_SSI 1 #endif #ifndef NGX_CRYPT #define NGX_CRYPT 1 #endif #ifndef NGX_HTTP_X_FORWARDED_FOR #define NGX_HTTP_X_FORWARDED_FOR 1 #endif #ifndef NGX_HTTP_X_FORWARDED_FOR #define NGX_HTTP_X_FORWARDED_FOR 1 #endif #ifndef NGX_HTTP_UPSTREAM_ZONE #define NGX_HTTP_UPSTREAM_ZONE 1 #endif #ifndef NGX_PCRE #define NGX_PCRE 1 #endif #ifndef NGX_ZLIB #define NGX_ZLIB 1 #endif #ifndef NGX_PREFIX #define NGX_PREFIX "/usr/local/nginx-1.19.3-dev/" #endif #ifndef NGX_CONF_PREFIX #define NGX_CONF_PREFIX "conf/" #endif #ifndef NGX_SBIN_PATH #define NGX_SBIN_PATH "sbin/nginx" #endif #ifndef NGX_CONF_PATH #define NGX_CONF_PATH "conf/nginx.conf" #endif #ifndef NGX_PID_PATH #define NGX_PID_PATH "logs/nginx.pid" #endif #ifndef NGX_LOCK_PATH #define NGX_LOCK_PATH "logs/nginx.lock" #endif #ifndef NGX_ERROR_LOG_PATH #define NGX_ERROR_LOG_PATH "logs/error.log" #endif #ifndef NGX_HTTP_LOG_PATH #define NGX_HTTP_LOG_PATH "logs/access.log" #endif #ifndef NGX_HTTP_CLIENT_TEMP_PATH #define NGX_HTTP_CLIENT_TEMP_PATH "client_body_temp" #endif #ifndef NGX_HTTP_PROXY_TEMP_PATH #define NGX_HTTP_PROXY_TEMP_PATH "proxy_temp" #endif #ifndef NGX_HTTP_FASTCGI_TEMP_PATH #define NGX_HTTP_FASTCGI_TEMP_PATH "fastcgi_temp" #endif #ifndef NGX_HTTP_UWSGI_TEMP_PATH #define NGX_HTTP_UWSGI_TEMP_PATH "uwsgi_temp" #endif #ifndef NGX_HTTP_SCGI_TEMP_PATH #define NGX_HTTP_SCGI_TEMP_PATH "scgi_temp" #endif #ifndef NGX_SUPPRESS_WARN #define NGX_SUPPRESS_WARN 1 #endif #ifndef NGX_SMP #define NGX_SMP 1 #endif #ifndef NGX_USER #define NGX_USER "nobody" #endif #ifndef NGX_GROUP #define NGX_GROUP "nobody" #endif
1、http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
nginx command type
#define NGX_CONF_NOARGS 0x00000001
#define NGX_CONF_TAKE1 0x00000002
#define NGX_CONF_TAKE2 0x00000004
#define NGX_CONF_TAKE3 0x00000008
#define NGX_CONF_TAKE4 0x00000010
#define NGX_CONF_TAKE5 0x00000020
#define NGX_CONF_TAKE6 0x00000040
#define NGX_CONF_TAKE7 0x00000080
#define NGX_CONF_TAKE12 (NGX_CONF_TAKE1|NGX_CONF_TAKE2)
#define NGX_CONF_TAKE13 (NGX_CONF_TAKE1|NGX_CONF_TAKE3)
#define NGX_CONF_TAKE23 (NGX_CONF_TAKE2|NGX_CONF_TAKE3)
#define NGX_CONF_TAKE123 (NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3)
#define NGX_CONF_TAKE1234 (NGX_CONF_TAKE1|NGX_CONF_TAKE2|NGX_CONF_TAKE3 \
|NGX_CONF_TAKE4)
#define NGX_CONF_ARGS_NUMBER 0x000000ff
#define NGX_CONF_BLOCK 0x00000100
#define NGX_CONF_FLAG 0x00000200
#define NGX_CONF_ANY 0x00000400
#define NGX_CONF_1MORE 0x00000800
#define NGX_CONF_2MORE 0x00001000
#define NGX_CONF_MULTI 0x00000000 /* compatibility */
#define NGX_DIRECT_CONF 0x00010000
#define NGX_MAIN_CONF 0x01000000
#define NGX_ANY_CONF 0x0F000000
http:
#define NGX_HTTP_MAIN_CONF 0x02000000
#define NGX_HTTP_SRV_CONF 0x04000000
#define NGX_HTTP_LOC_CONF 0x08000000
#define NGX_HTTP_UPS_CONF 0x10000000
#define NGX_HTTP_SIF_CONF 0x20000000
#define NGX_HTTP_LIF_CONF 0x40000000
#define NGX_HTTP_LMT_CONF 0x80000000
nginx的一个bug:
这样的一个请求:
GET / HTTP/1.1\r\n
Host:\r\n
\r\n
这个请求按照http1.1规范来看,应该是正确的,但是却返回400 Bad Request错误:
HTTP/1.1 400 Bad Request
Server: nginx/0.8.18
Date: Fri, 22 Mar 2019 14:56:14 GMT
Content-Type: text/html
Content-Length: 173
Connection: close
<html>
<head><title>400 Bad Request</title></head>
<body bgcolor="white">
<center><h1>400 Bad Request</h1></center>
<hr><center>nginx/0.8.18</center>
</body>
</html>
如果Host头随便指定一个值,就正确了:
GET / HTTP/1.1\r\n
Host:abc\r\n
\r\n
返回:
HTTP/1.1 200 OK
Server: nginx/0.8.18
Date: Fri, 22 Mar 2019 15:00:17 GMT
Content-Type: text/html
Content-Length: 151
Last-Modified: Wed, 30 Aug 2006 06:39:18 GMT
Connection: keep-alive
Accept-Ranges: bytes
<html>
<head>
<title>Welcome to nginx!</title>
</head>
<body bgcolor="white" text="black">
<center><h1>Welcome to nginx!</h1></center>
</body>
</html>
相关推荐
**Nginx版本升级步骤详解** 在Web服务器领域,Nginx以其高性能、低内存消耗以及高并发处理能力而备受青睐。随着新版本的发布,可能会包含性能优化、安全修复和新特性,因此定期更新Nginx版本是必要的。本文将详细...
arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构 docker运行nginx镜像包,arm 架构...
Nginx 1.24.0 是 Nginx 开源项目发布的一个重要更新版本,该版本在性能优化、功能增强以及安全性提升方面带来了诸多改进。当您下载 Nginx 1.24.0 的压缩包时,您将获得一个包含 Nginx 源代码的压缩文件,通常命名为 ...
nginx1.16镜像包 使用docker load -i xx.tar 拉取镜像 使用docker images 查看是否成功。 运行容器 docker run --name nginx-test -p 8080:80 -d nginx 参数说明: --name nginx-test:容器名称。 -p 8080:80: ...
在某些情况下,比如没有外网访问权限的内网环境,或者出于安全和稳定性考虑,用户可能需要在没有网络连接的情况下安装Nginx。离线安装Nginx意味着不能通过包管理器直接从互联网下载安装包和依赖,因此需要预先准备好...
现在,我们可以下载Nginx的源代码包`nginx-1.20.1.tar.gz`。你可以通过wget或者浏览器将文件下载到本地,然后解压: ```bash wget http://nginx.org/download/nginx-1.20.1.tar.gz tar -zxvf nginx-1.20.1.tar.gz cd...
Nginx以其高性能、稳定性、丰富的功能集以及易于配置和使用而闻名。在本文中,我们将深入了解Nginx版本1.27.3的相关知识,探讨其特性、配置方法以及在现代Web架构中的应用。 ### Nginx 1.27.3的新特性 版本1.27.3...
在探讨nginx依赖包资源下载的背景之下,我们首先要了解nginx是什么。Nginx是一款轻量级的Web服务器/反向代理服务器及电子邮件(IMAP/POP3)代理服务器。由于其高性能、稳定性、丰富的功能集以及简单的配置方式,...
Nginx是一个高性能的Web服务器和反向代理服务器,它以其高效的并发处理能力、低内存占用和稳定性而闻名。在1.23.2版本中,Nginx继续提供了优化和改进,以满足不断变化的互联网需求。这个版本可能是对之前版本的bug...
版本 1.26.2 是 Nginx 在 2023 年发布的新版本,它通常包含对各种协议的改进、性能优化以及安全问题的修复。 Docker 是一个开源的应用容器引擎,可以让开发者打包他们的应用以及应用的依赖包到一个可移植的容器中,...
这个名为"nginx-linux-arm64.zip"的压缩包提供的是专为ARM64架构(也称为AArch64)编译的Nginx版本,适用于基于Linux操作系统的64位ARM处理器设备,如树莓派、某些云服务器或嵌入式系统。无需繁琐的编译过程,只需...
nginx1.18镜像包 使用docker load -i xx.tar 拉取镜像 使用docker images 查看是否成功。 运行容器 docker run --name nginx-test -p 8080:80 -d nginx 参数说明: --name nginx-test:容器名称。 -p 8080:80: ...
这里我们关注的是一个包含特定模块的Nginx配置:`nginx1.16`,`nginx-upstream-check-module-master` 和 `nginx-upload-module`。这三个组件将帮助我们增强Nginx在处理后端服务健康检查、文件上传等方面的功能。 ...
由俄罗斯人Igor Sysoev所开发,其特点是占有内存少,并发能力强,事实上Nginx的并发能力确实在同类型的网页服务器中表现较好。Nginx已经在多数安装了PHP的应用中代替了Apache服务器,而由于其易于配置,轻量级,性能...
### Nginx 作为 Apache 和 JBoss 的替代方案 #### 背景介绍 随着互联网技术的不断发展,网站流量的增长对服务器性能提出了更高要求。Apache 和 JBoss 是两种广泛使用的 Web 服务器和应用服务器,但在高并发场景下,...
**Nginx 1.13.3 版本详解** Nginx 是一款高性能的 HTTP 和反向代理服务器,广泛应用于网站托管、负载均衡以及应用程序交付等领域。它以其高效、稳定和轻量级的特性著称,尤其在处理静态内容和高并发请求时表现优秀...
在2011年,Nginx的市场份额已经超越了Apache,成为世界上使用最多的Web服务器软件之一。Nginx以其高性能、稳定性、丰富的功能集、简单的配置文件和低资源消耗而闻名。Nginx的模块化架构设计使其具有高度的可扩展性,...
此资源有两个文件,含 nginx-upstream-jvm-route 和 nginx 对应版本,都是tar.gz文件。 安装方法网上很多就不写了,亲测可用。 不用担心版本不匹配造成安装失败,再浪费积分去到处下载尝试的烦恼。 此资源有两个文件...
在Linux系统中,离线安装Nginx是一个常见的需求,特别是在没有互联网连接或者网络环境受限的服务器上。本文将详细讲解如何通过离线方式在Linux上安装Nginx,同时也会涉及Nginx依赖的软件如openssl和gcc的安装过程。 ...
无标题nginx1.26.2的docker镜像文件实际上是一种以Docker容器形式封装的Nginx服务器软件。Docker是一个开源的应用容器引擎,允许开发者打包应用以及依赖包到一个可移植的容器中,然后发布到任何流行的Linux机器上,...