Nginx配置
nginx的配置文件是conf/nginx.conf
,如果存在nginx.conf.default
,则指的是默认的配置备份文件。
1 2 3 4 5 6 7
|
worker_processes 1; events { worker_connections 1024; }
|
启动nginx是有多个进程,其中master process
是主进程的意思,也叫做管理进程,它是用来管理nginx整个运行 的,nginx的其他子进程如果死掉了,它会自动在启动其他的子进程,比如尝试kill 下面的子进程,你会发 现另外一个进程又自动启动了。
真正干活的进程是下面的worker process进程,叫做工作进程,有请求来了 都是它处理的

默认nginx的站点根目录(存放网站代码的目录,也叫做网站的物理路径、真实路径等),一般在/nginx/html
其他的配置简单介绍
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| http { include mime.types; default_type application/octet-stream; charset utf-8; server { listen 80; server_name localhost; location /{ root html; index index.html index.htm; } } }
|
多端口部署
一个nginx
上可以运行多个网站http:// + ip/域名 + 端口
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http { include mime.types; default_type application/octet-stream; charset utf-8; server { listen 80; server_name localhost; location / { root /html/one; index index.html index.htm; } } server { listen 81; server_name localhost; location / { root /html/two; index index.html index.htm; } } }
|
多IP部署
每个操作系统其实都可以配置多个ip地址,多个ip地址一定要手动配置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http { include mime.types; default_type application/octet-stream; charset utf-8; server { listen 192.168.1.5:80; server_name localhost; location / { root /html/one; index index.html index.htm; } } server { listen 192.168.1.6:81; server_name localhost; location / { root /html/two; index index.html index.htm; } } }
|
多域名部署
如果在公网访问,可以配置多域名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| http { include mime.types; default_type application/octet-stream; charset utf-8; server { listen 80; server_name aaa.com; location / { root /html/one; index index.html index.htm; } } server { listen 80; server_name bbbb.com; location / { root /html/two; index index.html index.htm; } } }
|
include配置文件
如果需要配置的网站太多,所有的配置都放在nginx.conf
中会非常难以维护,所以可以对每个网站分开配置,然后在nginx.conf
中引入配置
nginx.conf主配置文件
1 2 3 4 5 6 7 8 9 10
| worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; charset utf-8; include /web/custom/*.conf; }
|
其他配置文件,如test.conf
1 2 3 4 5 6 7 8
| server { listen 80; server_name bbbb.com; location / { root /html/two; index index.html index.htm; } }
|
如果访问的域名不能正确匹配到网站时,会自动匹配上一个能匹配上的网站,但是这样不太友好,最好指定一下默认网站
1 2 3 4
| server { listen 80 default_server; ... }
|
nginx日志
默认的日志在logs
目录下,
我们可以自定义日志格式,这个必须在server配置外面来配置,后面的都是nginx变量,后面详细讲
log_format compression '$remote_addr - $remote_user [$time_local] ' '"$request" $status $bytes_sent ' '"$http_referer" "$http_user_agent" "$gzip_ratio"';
compression
是名称
1 2 3 4 5 6 7 8 9
| server { listen 80; server_name test.com; access_log /opt/nginx/b.jaden.com_log test; location / { root /web/two; index index.html index.htm; } }
|
basicr认证
创建一个文件,保存htpasswd
的密码,如:jaadsn:FdHiKZYkjLx
修改server配置
1 2 3 4 5 6 7 8 9 10
| server { listen 80; server_name b.jaden.com; location / { auth_basic "baaa.com"; auth_basic_user_file /etc/nginx/htpasswd; root /web/two; index index.html index.htm; } }
|
SSL证书配置
- 下载证书,放到某目录下如
cert
- 配置nginx
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server { listen 443 ssl; server_name b.jaden.com; ssl_certificate cert/<cert-file-name>.pem; ssl_certificate_key cert/<cert-file-name>.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on location / { auth_basic "baaa.com"; auth_basic_user_file /etc/nginx/htpasswd; root /web/two; index index.html index.htm; } }
|
return和rewrite
1 2 3 4 5 6 7 8
| server { access_log off; listen 80; server_name www.aaaa.top; location / { return 302 https://www.aaa.top$request_uri; } }
|
1 2 3 4 5 6 7 8 9
| server { access_log off; listen 80; server_name www.aaa.top; location / { rewrite ^/(.*) https://www.aaa.top/$1 redirect; } }
|
gzip压缩
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 80; server_name a.jaaa.com; access_log /opt/nginx/a.aa.com_log aa; location / { gzip on; gzip_min_length 1k; gzip_buffers 4 32k; gzip_http_version 1.1; gzip_comp_level 9; gzip_types text/html text/css text/xml application/javascript; gzip_vary on; gzip_disable "MSIE [1-7]\."; root /web/one; index index.html index.htm; } }
|
nginx目录浏览
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| autoindex on; autoindex_exact_size off;
server { listen 80 default_server; server_name c.aaa.com; autoindex on; autoindex_exact_size off; location / { root /web/three; index index.html index.htm; } }
|
控制访问(黑白名单)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| server { listen 80 default_server; server_name c.aaa.com; location / { deny 192.168.61.1; allow 0.0.0.0/0; root /web/three; index index.html index.htm; } }
deny 192.168.61.1; allow 0.0.0.0/0;
allow 192.168.61.1; allow 192.168.61.16;
deny 0.0.0.0/0;
|
location和优先级
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| server { listen 80 default_server; server_name c.aaa.com; location / { root /web/three; index index.html index.htm; } location /a { auth_basic "b.jaden.com"; auth_basic_user_file /etc/nginx/htpasswd; } }
server { listen 80 default_server; server_name c.aaa.com; location / { root /web/three; index index.html index.htm; } location /a { root /abc } }
|
location的路径支持正则写法,了解几个简单的即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| server { listen 80 default_server; server_name c.aaa.com; location / { root /web/three; index index.html index.htm; } location ~* ^.*\.txt$ { return 404; } }
location ~* /a+/ { return 405; } location ~ /a+/ { return 405; } location = /1.txt { return 405; }
|
nginx常用变量
变量 | 说明 |
---|
$args | 请求中的参数,也叫查询参数,如 www.123.com/1.php?a=1&b=2的$args就是a=1&b=2 |
$content_length | HTTP响应信息里的”Content-Length” |
$content_type | HTTP响应信息里的”Content-Type”,文本文件在浏览上是可以直接预览 的,就是可以直接打开 |
$document_root | nginx虚拟主机配置文件中的root站点根目录 |
$document_uri | 当前请求中不包含指令的URI,如www.123.com/1.php?a=1&b=2的 $document_uri就是/1.php,不包含后面的参数 |
$host | 主机头,也就是域名或者ip地址 |
$http_user_agent | 客户端的详细信息,也就是浏览器的标识 |
$http_cookie | 客户端的cookie信息 |
$limit_rate | 如果nginx服务器使用limit_rate配置了显示网络速率,则会显示,如果没 有设置, 则显示0 |
$remote_addr | 客户端的公网ip |
$remote_port | 客户端的port |
$remote_user | 如果nginx有配置认证,该变量代表客户端认证的用户名 |
$request_body_file | 做反向代理时发给后端服务器的本地资源的名称 |
$request_method | http请求方法,GET/POST/PUT/DELETE等 |
$request_filename | 当前请求的资源文件的路径名称,相当于是 $document_root/$document_uri的组合 |
$request_uri | 请求的链接,包括$document_uri和$args |
$scheme | 请求的协议,如ftp,http,https |
$server_protocol | 客户端请求资源使用的协议的版本,如HTTP/1.0,HTTP/1.1,HTTP/2.0 等 |
$server_addr | 服务器IP地址 |
$server_name | 服务器的主机名 |
$server_port | 服务器的端口号 |
$uri | 和$document_uri相同 |
$http_referer | 客户端请求时的referer请求头键值对的值,通俗讲就是该请求是通过哪个 链接跳过来的,用curl -e可以指定 |
防盗链
1 2 3 4 5 6 7 8 9 10 11 12 13
| server { listen 80 ; server_name c.aaa.com; location / { root /web/three; index index.html index.htm; } location ~* \.png$ { if ( $http_referer !~* "c.aaa.com" ) { return 403; } } }
|
自动中英文
1 2 3 4 5 6 7 8 9 10
| server { listen 80 ; server_name c.aaa.com; location / { if ( $http_accept_language ~* ^en ) { root /html/lang/en; } root /html/lang/cn; } }
|
动态网站
主要是针对前后端不分离的类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| server { listen 80 default_server; server_name c.aaa.com; location / { root /web/three; index index.html index.htm; } location ~ \.php${ root /web/three; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /html/wulaaaa$fastcgi_script_name; include fastcgi_params; } }
|
反向代理
1 2 3 4 5 6 7 8 9 10 11
| server { listen 80 default_server; server_name c.aaa.com; location / { proxy_pass http://aaa.com; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $remote_addr; } }
|
负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13
| upstream firstdemo { server 192.168.31.123:8081 weight=1; server 192.168.31.123:8082 weight=2; ip_hash; }
server { listen 8080; server_name localhost; location / { proxy_pass http://firstdemo; } }
|