记录一下第一次安装和配置Nginx

前言

Nginx虽不是第一次接触,但是使用较少,今天算是完整的安装了配置了一次,前段时间想写一写的《Nginx的安装与常用配置》,因突发状况没有写完,今天补充一下。

安装 nginx

nginx 安装比较方便,我安装的环境是拥有root权限的Ubuntu 20.04,安装只需要以下两个命令:

1
2
apt update
apt install nginx

安装完默认启动,查询运行状态的命令 systemctl status nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2023-03-14 21:09:35 CST; 2h 55min ago
Docs: man:nginx(8)
Process: 943307 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Process: 943314 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
Main PID: 943315 (nginx)
Tasks: 5 (limit: 9132)
Memory: 10.6M
CGroup: /system.slice/nginx.service
├─943315 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
├─943316 nginx: worker process
├─943317 nginx: worker process
├─943318 nginx: worker process
└─943319 nginx: worker process

Mar 14 21:09:35 w-mi-dev systemd[1]: Starting A high performance web server and a reverse proxy server...
Mar 14 21:09:35 w-mi-dev snoopy[943307]: [hostname:w-mi-dev ssh:((undefined)) uid:0 username:root pid:943307 ppid:1 rpname:(nginx) sid:943307 tty:(none) cwd:/ filename:/usr/sbin/nginx]: /usr/sbin/ng>
Mar 14 21:09:35 w-mi-dev snoopy[943314]: [hostname:w-mi-dev ssh:((undefined)) uid:0 username:root pid:943314 ppid:1 rpname:(nginx) sid:943314 tty:(none) cwd:/ filename:/usr/sbin/nginx]: /usr/sbin/ng>
Mar 14 21:09:35 w-mi-dev systemd[1]: Started A high performance web server and a reverse proxy server.

要想测试的话通过浏览器访问 http://YOUR_IP,通常会看到 “Welcome to nginx” 字样的界面,但这对于我是没用的,我的默认端口访问不了。

查找nginx安装和配置文件的位置

我的nginx安装在 /usr/sbin/nginx 目录,配置文件主目录在 /etc/nginx/nginx.conf,找的方法比较简单就是用 which 命令

1
2
# which nginx
/usr/sbin/nginx

找配置文件时利用 nginx 检查配置文件的 -t 参数就可以了

1
2
3
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

如果使用 which 命令找不到,可以利用 ps -ef | grep nginx 找到进程id,在利用 ll /proc/nginx_pid 来找到安装的位置。

修改配置文件

进入 /etc/nginx 目录,可以看到多个配置文件和目录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# cd /etc/nginx/
# ll
total 64
drwxr-xr-x 2 root root 4096 Mar 14 20:56 conf.d/
-rw-r--r-- 1 root root 1077 Feb 4 2019 fastcgi.conf
-rw-r--r-- 1 root root 1007 Feb 4 2019 fastcgi_params
-rw-r--r-- 1 root root 2837 Feb 4 2019 koi-utf
-rw-r--r-- 1 root root 2223 Feb 4 2019 koi-win
-rw-r--r-- 1 root root 3957 Feb 4 2019 mime.types
drwxr-xr-x 2 root root 4096 Nov 10 14:38 modules-available/
drwxr-xr-x 2 root root 4096 Mar 9 21:30 modules-enabled/
-rw-r--r-- 1 root root 1490 Feb 4 2019 nginx.conf
-rw-r--r-- 1 root root 180 Feb 4 2019 proxy_params
-rw-r--r-- 1 root root 636 Feb 4 2019 scgi_params
drwxr-xr-x 2 root root 4096 Mar 9 21:30 sites-available/
drwxr-xr-x 2 root root 4096 Mar 9 21:30 sites-enabled/
drwxr-xr-x 2 root root 4096 Mar 9 21:30 snippets/
-rw-r--r-- 1 root root 664 Feb 4 2019 uwsgi_params
-rw-r--r-- 1 root root 3071 Feb 4 2019 win-utf

其中 nginx.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
worker_connections 768;
# multi_accept on;
}

http {

##
# Basic Settings
##

sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# server_tokens off;

# server_names_hash_bucket_size 64;
# server_name_in_redirect off;

include /etc/nginx/mime.types;
default_type application/octet-stream;

##
# SSL Settings
##

ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;

##
# Logging Settings
##

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

# Gzip Settings
##

gzip on;

# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

##
# Virtual Host Configs
##

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}


#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}

这个文件是默认的配置文件,我们可以先不管,先注意几点就行:

  • 访问日志在 /var/log/nginx/access.log
  • 错误日志在 /var/log/nginx/error.log
  • 主配置文件包含了 /etc/nginx/conf.d/*.conf 这个目录下所有配置

所以我们在 /etc/nginx/conf.d/ 目录下新建一个 project.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
upstream login_entrance {
server localhost:4101;
server localhost:4102;
}

upstream exit_entrance {
server localhost:6101;
server localhost:6102;
}

server {
listen 4100;
server_name localhost;

location / {

proxy_pass http://login_entrance;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
listen 6100;
server_name localhost;

location / {

proxy_pass http://exit_entrance;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

server {
listen 8100;
server_name localhost;

location / {
root /usr/share/nginx/html;
}
}

配置文件的含义:

  • 访问 8100 端口可以看到 nginx 欢迎页
  • 将发送给4100端口的数据转发给4101和4102端口
  • 将发送给6100端口的数据转发给6101和6102端口

重启nginx服务

重启可采用关闭服务器再开启的方式,也可以直接使用以下命令:

  • systemctl restart nginx
  • nginx -s reload

查看报错

当nginx启动不起来是可以通过 systemctl status nginx 命令查看,也可以直接查看 errorlog 文件,比如可能会看到如下内容:

1
2
3
4
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)
2023/03/14 20:51:06 [emerg] 937503#937503: bind() to 0.0.0.0:4100 failed (98: Address already in use)

一看就应该知道是端口4100被占用了。

负载均衡配置

负载均衡是nginx一个常见的应用,只需要在配置文件中加入一些参数即可:

轮询

这是默认的策略,把每个请求按次序逐一分配到不同可用的server,如果分配到的server不可用,则自动分配到下一个

1
2
3
4
upstream good.cc {
server localhost:4101;
server localhost:4102;
}

权重

weight默认值为1,值越大则代表被访问的几率越大,下面配置中4102端口的访问几率是4101的二倍

1
2
3
4
upstream good.cc {
server localhost:4101 weight=1;
server localhost:4102 weight=2;
}

最少连接

顾名思义就是把请求分配到连接数最少的server

1
2
3
4
5
upstream good.cc {
least_conn;
server localhost:4101;
server localhost:4102;
}

IP哈希

根据访问客户端ip的hash值分配,这样同一客户端的请求都会被分配到同一个server上,适合有状态的服务

1
2
3
4
5
upstream good.cc {
ip_hash;
server localhost:4101;
server localhost:4102;
}

总结

  • nginx 在 Ununtu 20.04 上安装非常简单,使用 apt install nginx 就行啦
  • nginx 的默认主配置文件是 /etc/nginx/nginx.conf 会包含同级目录下 conf.d/*.conf 所有配置
  • nginx 服务可以用 systemctl status nginx 方式查看
  • nginx 的重启方式 systemctl restart nginxnginx -s reload
  • nginx 作为负载均衡服务器有轮询、权重、最少连接、ipHash等多种配置方式
  • 我也是刚刚入门,关于使用方法和配置文件还有很多东西要学,后面慢慢总结吧

==>> 反爬链接,请勿点击,原地爆炸,概不负责!<<==

无善无恶心之体,有善有恶意之动,知善知恶是良知,为善去恶是格物,愣凑数啊~

Albert Shi wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客