Nginx系统入门

Nginx系统入门

Nginx配置

nginx的配置文件是conf/nginx.conf,如果存在nginx.conf.default,则指的是默认的配置备份文件。

1
2
3
4
5
6
7
#启动nginx时工作进程的数量,可以加大这个数字来提高nginx的处理请求的效率,但是这个数字也不能太大,因为进程是消耗系统内存资源的。
# 建议服务器是几核就设置几
worker_processes 1;
events {
#连接数量,每个进程可以处理1024连接
worker_connections 1024;
}

启动nginx是有多个进程,其中master process是主进程的意思,也叫做管理进程,它是用来管理nginx整个运行 的,nginx的其他子进程如果死掉了,它会自动在启动其他的子进程,比如尝试kill 下面的子进程,你会发 现另外一个进程又自动启动了。

真正干活的进程是下面的worker process进程,叫做工作进程,有请求来了 都是它处理的

image-20250729144020954

默认nginx的站点根目录(存放网站代码的目录,也叫做网站的物理路径、真实路径等),一般在/nginx/html

其他的配置简单介绍

1
2
3
4
5
6
7
8
9
10
11
12
13
14
http {
# include的意思是,nginx先加载一下mime.types文件中的配置
include mime.types; # 这个文件用来标识支持哪些多媒体格式
default_type application/octet-stream; #如果不能识别的文件,那么默认以八进制数据流的方式来打开文件
charset utf-8;
server {
listen 80; #监听端口
server_name localhost; # 域名
location /{
root html; # 根目录,可以是绝对目录,也可以是相对目录,相对目录对应nginx的根目录——/nginx/html
index index.html index.htm; # 默认首页,与mine.types中有配置
}
}
}

多端口部署

一个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地址一定要手动配置

image-20250729150749513

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; # 加载外部目目录下的所有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; # test是上面指定的日志格式的名称,可以配置每个网站的目录
location / {
root /web/two;
index index.html index.htm;
}
}

basicr认证

  1. 创建一个文件,保存htpasswd的密码,如:jaadsn:FdHiKZYkjLx

  2. 修改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证书配置

  1. 下载证书,放到某目录下如cert
  2. 配置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;
#表示使用的TLS协议的类型,您需要自行评估是否配置TLSv1.1协议,不加TLSv1.1就删掉
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

  • 使用return,推荐
1
2
3
4
5
6
7
8
server {
access_log off; # 这段配置是专门用来做跳转用的,所以日志就不用记录了,off就是关闭跳转行为的日志记录
listen 80;
server_name www.aaaa.top;
location / {
return 302 https://www.aaa.top$request_uri; # 当用户访问80端口时,自动跳转到https网址。
}
}
  • rewrite,写法支持很多,要写正则
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; # redirect代表302状态码,临时跳转,^代表网https://www.wulaoban.top,/(.*)其实就是匹配uri,$1表示()中匹配到的内容,也就是.*匹配到的内容
# rewrite ^/(.*) https://www.wulaoban.top/$1 permanent; # permanent代表301状态码,永久跳转
}
}

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压缩
gzip_min_length 1k; #最小压缩文件
gzip_buffers 4 32k; #内存缓冲
gzip_http_version 1.1; #http版本
gzip_comp_level 9; #压缩等级
gzip_types text/html text/css text/xml application/javascript; #压缩类型
gzip_vary on; #http响应头添加gzip标识
gzip_disable "MSIE [1-7]\."; # 遇到IE浏览器1-7取消gzip压缩
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; #显示文件大小的时候带单位
# 配置在nginx网站配置的server配置中:
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;# 黑名单,不允许192.168.61.1访问
allow 0.0.0.0/0; # 白名单,0.0.0.0/0所有ip都可以访问
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;
#allow 192.168.61.16/24; # 还可以写地址段
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;
}
# 当访问/a时,要认证
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;
}
# 当访问/a时,root改变
location /a {
root /abc
}
}

location的路径支持正则写法,了解几个简单的即可

1
2
3
4
5
	#没有符号,代表模糊匹配,不支持正则 location /te 可以匹配te开头的目录和文件
~ #表示执行一个正则匹配,区分大小写
~* #表示执行一个正则匹配,不区分大小写
= #针对的是文件,精准匹配,不支持正则
#符号优先级 = 大于 ~ 大于 ~* 大于 无符号
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;
}
# 下面正则的意思是,只要用户访问txt文件,都返回404状态码。那么就可以做到各类文件的保护,或者各种路径访问的控制
location ~* ^.*\.txt$ {
return 404;
}
}
# 再比如:只要a/A开头的目录,不区分大小写,都不能访问
location ~* /a+/ {
return 405; # 405是不允许访问的意思的状态码
}
# 再比如:只要a开头的目录,区分大小写,不能访问
location ~ /a+/ {
return 405;
}
# 再比如:不能访问1.txt文件
location = /1.txt {
return 405;
}

nginx常用变量

变量说明
$args请求中的参数,也叫查询参数,如 www.123.com/1.php?a=1&b=2的$args就是a=1&b=2
$content_lengthHTTP响应信息里的”Content-Length”
$content_typeHTTP响应信息里的”Content-Type”,文本文件在浏览上是可以直接预览 的,就是可以直接打开
$document_rootnginx虚拟主机配置文件中的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_methodhttp请求方法,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 ) { # 如果accept_language的值以en开头,也就是英文,那么返回英文的站点目录,否则返回中文的站点目录,而且有时候会根据ip地址来返回不同语言的网站。
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; # 当目录被请求时默认使用 index.php 作为首页
# 告诉 PHP-FPM 要执行哪个 PHP 文件
# $fastcgi_script_name 是请求的路径部分(如 /index.php)
fastcgi_param SCRIPT_FILENAME /html/wulaaaa$fastcgi_script_name;
#加载默认的 FastCGI 参数列表
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; #设置转发请求时的 Host 头
proxy_set_header X-Real-IP $remote_addr; #设置头部 X-Real-IP 为客户端真实 IP
proxy_set_header X-Forwarded-For $remote_addr; #设置 X-Forwarded-For 为原始客户端 IP
#X-Real-IP 通常只保存客户端的真实 IP,而 X-Forwarded-For 会记录整个代理链上的 IP 列表
}
}

负载均衡

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; #让相同的客户端ip请求相同的服务器
}

server {
listen 8080;
server_name localhost;
location / {
proxy_pass http://firstdemo;
}
}
作者

步步为营

发布于

2025-07-29

更新于

2025-07-29

许可协议