安装

手动编译

yun + rpm包

查看 linux 发行版本

1
2
3
4
5
6
7
[]$ lsb_release -a

LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core

对应rpm包下载地址 nginx rpm 包

三种安装方式

  • 线上下载 rpm 包 然后安装 (要自己解决依赖问题)
1
sudo rpm -Uvh http://nginx.org/packages/centos/7/x86_64/RPMS/nginx-1.20.2-1.el7.ngx.x86_64.rpm
  • 离线 通过下载好的 rpm文件 使用 yum 安装 (无须手动解决依赖问题)
1
yum install nginx-1.22.0-1.el7.ngx.x86_64.rpm
  • 官网教程

linux_package

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
sudo yum install yum-utils
sudo vim /etc/yum.repos.d/nginx.repo

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

sudo yum install -y nginx

yum 使用

1
2
3
4
5
6
7
8
9
# 列出 软件包
# --showduplicates 参数 表示展示出 多版本
yum list <package_name>
# 已经安装的
yum list installed <package_name>
# 可安装的
yum list available <package_name>
# 在当前源内搜索软件包
yum --showduplicates search nginx

docker

Dockerfile

1
2
3
4
5
6
FROM nginx
RUN mkdir -p /tmp/nginx
# https 配置文件
ADD ./https/* /tmp/nginx/
EXPOSE 80
EXPOSE 443

docker compose

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
version: "3.8"
services:
nginx:
container_name: myNginx
build:
context: .
dockerfile: Dockerfile
image: jyxzwd/nginx:latestcd
volumes:
# 挂载页面
- ./html:/usr/myhtml
# 挂载配置
- ./conf.d:/etc/nginx/conf.d
ports:
- "80:80"
- "443:443"
logging:
driver: "json-file"
options:
max-size: "2g"
max-file: "10"
restart: always

default.conf

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
server {

listen 80;
listen [::]:80;
server_name domain;

if ($host = 'langyu.world') {
rewrite ^/(.*)$ https://domain/$1 permanent;
}

#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 /usr/share/nginx/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;
#}
}



https.conf

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 443 ssl http2;
listen [::]:443 http2;
server_name domain;

ssl_certificate /tmp/nginx/domain_crt;
ssl_certificate_key /tmp/nginx/domain_key;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;


access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log;

location / {
root /usr/myhtml;
try_files $uri $uri/ /index.html
index index.html index.htm;
}


}

command sudo docker compose -f compose.y


nginx 操作

  • 启动 systemctl start nginx

  • 查看运行状态 systemctl status nginx

screen-capture

master 进程 通常 是属于root 的,用来管理 其他的 worker 进程,worker进程数在配置文件中国指定,通常数量为cpu核心数

screen-capture

  • nginx -s [stop | quit | reload | reopen ] 需要root权限 由 master进程执行

nginx配置

location 块

Synatx: location [ = | ~ | ~* | ^~ ] uri { . . .}

Context: http server

匹配规则

  • 首先去顺序寻找匹配 前缀字符串 ,并且记录下来 最长前缀的location
  • 然后去顺序寻找匹配正则表达式的字符串,在找到第一个匹配之后终止 A
  • 如果 最长前缀的location^~ 修饰,则不会再去 顺序寻找匹配正则表达式的字符串
  • 如果在 正则表达式的字符串中没有找到任何一个匹配,则使用前面记录的最长前缀字符串所在location定义的configuration B
  • 在以上过程中,一旦 =定义的location块 被匹配,则终止匹配 C

exercise.conf

screen-capture

几种情况

C-情况

curl -iv http://localhost:81/doc

screen-capture

找到 =就立马终止

A 情况

screen-capture

curl -iv http://localhost:81/doc/index.html


B-情况

curl -iv http://localhost:81/do

screen-capture


root

指定静态资源目录,可用在 http, server, location 块

Syntax: root path

Default: html

Context: http, server, location, if in location

1
2
3
4
location /image {
root /home/static;
}
langyu.world/image/1.jpg -> /home/static/image/1.jpg

相当于 root指定路径与 URI 叠加,如果要更改URI,则使用 alias

alias

Syntax: alias path

Default: -------

Context: location


重命名指向 只能用在 location

注意alias后面要加 '/'

1
2
3
4
5
6
7
8
location /music { 
alias /home/static/blog_music/;
}
langyu.world/music/jyx.mp3 -> /home/static/blog_music/jyx.mp3

location ~ ^/user/(.+\.(?:png|jpe?g|gif))$ {
alias /data/images/$1;
}

rewrite

重写 URL ,用在 location, server, if

1
2
rewrite <regex> <replacement> [flag]
regex 正则表达式 匹配

eg: rewrite ^/(.*)$ https://domain/$1 permanent

nginx解决跨域

前端部署的域名与端口和要访问的后端api域名和端口保持一致

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
http {
upstream server {
server localhost:8081 weigth=3;
server localhost:8082 weigth=2;
server localhost:8083 weigth=1;
}
server {
listen 443 ssl http2;
server_name localhost;

location /html/ {
root html;
}
location /api/v2 {
proxy_pass https://server;
}
}
}



https配置

yum 下载的nginx 默认预装了ssl模块

1
2
3
nginx -V 
...
configure arguments: --with-http_ssl_module
  • 配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 443 ssl http2 default_server; # SSL 访问端口号为 443
server_name domain; # 填写绑定证书的域名
ssl_certificate /etc/nginx/https/domain.crt; # 证书地址
ssl_certificate_key /etc/nginx/https/domain.key; # 私钥地址
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # 支持ssl协议版本,默认为后三个,主流版本是[TLSv1.2]

location / {
root /home/nginx/html/blog;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}

}