Nginx浅识

admin 2022年1月6日01:41:37评论32 views字数 8125阅读27分5秒阅读模式

Nginx (“engine x”) 是一个高性能的 HTTP 和反向代理服务器,特点是占有内存少,并发能力强。

安装和配置

centos

安装

在/etc/yum.repos.d目录下面创建一个nginx的yum

1
2
cd /etc/yum.repos.d #切换到yum安装源文件目录
vi nginx.repo #创建一个安装包源

复制一下内容

1
2
3
4
5
6
7
8
9
10
11
12
13
[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

[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

查看是否成功加载这个安装源

1
yum repolist

开始安装

1
yum install -y nginx

启动/停止/重启服务

1
2
3
systemctl start/stop/restart nginx.service #在centos7+ 启动/停止/重启nginx服务
service nginx start/stop/restart #启动/停止/重启
nginx -s stop/reload/quit #粗鲁的停止/重载/优雅停止

其他命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
[ye1s@localhost yum.repos.d]$ nginx -h
nginx version: nginx/1.18.0
Usage: nginx [-?hvVtTq] [-s signal] [-c filename] [-p prefix] [-g directives]

Options:
-?,-h : this help
-v : show version and exit
-V : show version and configure options then exit
-t : test configuration and exit
-T : test configuration, dump it and exit
-q : suppress non-error messages during configuration testing
-s signal : send signal to a master process: stop, quit, reopen, reload
-p prefix : set prefix path (default: /etc/nginx/)
-c filename : set configuration file (default: /etc/nginx/nginx.conf)
-g directives : set global directives out of configuration file

配置

目录文件

1
2
3
[ye1s@localhost nginx]$ ls
conf.d koi-utf mime.types nginx.conf uwsgi_params
fastcgi_params koi-win modules scgi_params win-utf

ubuntu

安装

1
apt-get install nginx

配置

目录文件

1
2
3
4
5
ye1s@ye1s:/etc/nginx$ ls
conf.d koi-utf modules-enabled sites-available win-utf
default koi-win nginx.conf sites-enabled
fastcgi.conf mime.types proxy_params snippets
fastcgi_params modules-available scgi_params uwsgi_params

nginx.conf

以下来分析一下nginx.conf这个文件中的内容
nginx配置文件分为三大块,全局块,events块,http块。

全局块

1
2
3
4
5
6
7
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

这里主要会设置一些影响 nginx 服务器整体运行的配置指令,主要包括配 置运行 Nginx 服务器的用户(组)、允许生成的 worker process 数,进程 PID 存放路径、日志存放路径和类型以及配置文件的引入等。

比如 worker_processes auto; 这一行,worker_processes 值越大,我们nginx可支持的并发数量就越多

events 块

1
2
3
events {
worker_connections 1024;
}

events 块涉及的指令主要影响 Nginx 服务器与用户的网络连接,常用的设置包括是否开启对多 work process
下的网络连接进行序列化,是否允许同时接收多个网络连接,选取哪种事件驱动模型来处理连接请求,每个 word
process 可以同时支持的最大连接数等。

http块

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

http {
include /etc/nginx/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 /var/log/nginx/access.log main;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
}

http 全局块配置的指令包括文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等。
而http块中的server块则相当于一个虚拟主机,一个http块可以拥有多个server块。
server块又包括全局server块,和location块。
全局server块主要包括了本虚拟机主机的监听配置和本虚拟主机的名称或 IP 配置
location块则用来对虚拟主机名称之外的字符串进行匹配,对特定的请求进行处理。地址定向、数据缓存和应答控制等功能,还有许多第三方模块的配置也在这里进行。比如,对/usr相关的请求交给8080来处理,/admin则较给8081处理。

Nginx配置

设置只允许域名访问

1
2
3
4
5
listen       80;
server_name www.xuqian8.com;
if ($host != 'www.xuqian8.com'){
return 403;
}

thinkphp配置

config.php中设置

1
2
3
/* URL配置 */
'URL_CASE_INSENSITIVE' => true, // 默认false 表示URL区分大小写 true则表示不区分大小写
'URL_MODEL' => 2, // URL模式

nginx.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {

listen 80;
server_name src.hillstonenet.com;
root /var/www/xsrc; # 该项要修改为你准备存放相关网页的路径
index index.html index.htm index.php l.php;
location / {
root /var/www/xsrc;
index index.php index.html index.htm;
}

#proxy the php scripts to php-fpm
location ~ .+\.php($|/) {
root /var/www/xsrc;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}

配置vue

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
server {
listen 8888;
listen [::]:8888;
server_name localhost;
root /var/www/search/frontend;
index index.html index.htm;

location / {
try_files $uri $uri/ @router;
index index.html index.htm;
}

location @router {
rewrite ^.*$ /index.html last;
}
}

配置 django

pip安装 uwsgi

1
pip3 install uwsgi

mysite.ini

1
2
3
4
5
6
7
8
9
[uwsgi]
socket = 127.0.0.1:9901
chdir = /var/www/search/backend
module = backend.wsgi
master = true
processes = 10
threads = 40
vacuum = true
daemonize = /tmp/uwsgi.log #输出信息打印在日志

启动

1
uwsgi --ini mysite.ini

nginx配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
server {
listen 8000;
server_name 127.0.0.1
charset UTF-8;
access_log /var/log/nginx/backend_access.log;
error_log /var/log/nginx/backend_error.log;
client_max_body_size 75M;

location / {
include uwsgi_params;
uwsgi_pass 127.0.0.1:9901;
uwsgi_read_timeout 2;
}

}

注意点
setting.py 的ALLOWED_HOSTS配置为*,要不会出现Bad request 400

1
2
3
DEBUG = False

ALLOWED_HOSTS = ['*']

出现502错误

1
2
sudo apt-get install uwsgi-plugin-python3 
`

mysite.ini文件中添加

1
plugins=python3

添加证书

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
#80重定向443
server {
listen 80;
server_name src.hillstonenet.com;
rewrite ^(.*)$ https://src.hillstonenet.com/;
}
server {

listen 443 ssl;
server_name src.hillstonenet.com;
root /var/www/xsrc; # 该项要修改为你准备存放相关网页的路径
index index.html index.htm index.php l.php;
ssl_certificate cert/1_src.hillstonenet.com_bundle.crt;
ssl_certificate_key cert/2_src.hillstonenet.com.key;
ssl_session_timeout 1m;
location / {
root /var/www/xsrc;
index index.php index.html index.htm;
}

#proxy the php scripts to php-fpm
location ~ .+\.php($|/) {
root /var/www/xsrc;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
~
~

限制ip访问速率

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 ngx_http_limit_req_module:限制某一时间内,单一IP的请求数.示例:

复制代码

http {
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;

...
  #定义一个名为one的limit_req_zone用来存储session,大小是10M内存,  

  #以$binary_remote_addr 为key,限制平均每秒的请求为1个,
  #1M能存储16000个状态,rete的值必须为整数,
  

server {

...

location /search/ {
limit_req zone=one burst=5;
        
        #限制每ip每秒不超过1个请求,漏桶数burst为5,也就是队列.
        #nodelay,如果不设置该选项,严格使用平均速率限制请求数,超过的请求被延时处理.

反向代理

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
server {
listen 8888 ; ##设置我们nginx监听端口为8888
server_name [服务器的ip地址];

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location /hi/ {
proxy_pass http://127.0.0.1:8080; ##需要代理的服务器地址
index index.html;
}

location /hello/ {
proxy_pass http://127.0.0.1:8081; ##需要代理的服务器地址
index index.html;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

在浏览器中输入:服务器ip:8888/hi/hi.html
浏览器显示 I am hi 对应服务器端口为 8080
在浏览器中输入:服务器ip:8888/hello/hello.html
浏览器显示 I am hello 对应服务器端口为 8081
从而实现了针对不同url请求分发给不同服务器的功能配置。

location指令说明:
功能:用于匹配URL
语法如下:

1
2
3
4
5
6
7
1、= :用于不含正则表达式的 uri 前,要求请求字符串与 uri 严格匹配,如果匹配
成功,就停止继续向下搜索并立即处理该请求。
2、~:用于表示 uri 包含正则表达式,并且区分大小写。
3、~*:用于表示 uri 包含正则表达式,并且不区分大小写。
4、^~:用于不含正则表达式的 uri 前,要求 Nginx 服务器找到标识 uri 和请求字
符串匹配度最高的 location 后,立即使用此 location 处理请求,而不再使用 location
块中的正则 uri 和请求字符串做匹配。

负载均衡

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
http {

###此处省略一大堆没有改的配置


##自定义我们的服务列表
upstream myserver{
server 127.0.0.1:8080;
server 127.0.0.1:8090;
}


server {
listen 8888 ; ##设置我们nginx监听端口为8888
server_name [服务器的ip地址];

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
proxy_pass http://myserver; ##叮,核心配置在这里
proxy_connect_timeout 10; #超时时间,单位秒
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

}

nginx提供了三种不同的负载均衡策略供我们灵活选择,分别是:

轮询(默认方式): 每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器 down 掉,能自动剔除。
用法:啥也不加,上文实例就是默认的方式,就是默认的

权重(weight): weight 代表权重,默认为 1,权重越高被分配的客户端越多,权重越大,能力越大,责任越大,处理的请求就越多。
用法:

1
2
3
4
upstream myserver{
server 127.0.0.1:8080 weight =1;
server 127.0.0.1:8090 weight =2;
}

ip_hash:每个请求按访问 ip 的 hash 结果分配,这样每个访客固定访问一个后端服务器,可以解决 session 的问题。
用法:

1
2
3
4
5
upstream myserver{
ip_hash;#可与weight配合使用
server 127.0.0.1:8080 weight =1;
server 127.0.0.1:8090 weight =2;
}

动静分离

动静分离就是把很少会发生修改的诸如图像,视频,css样式等静态资源文件放置在单独的服务器上,而动态请求则由另外一台服务器上进行,这样一来,负责动态请求的服务器则可以专注在动态请求的处理上,从而提高了我们程序的运行效率,与此同时,我们也可以针对我们的静态资源服务器做专属的优化,增加我们静态请求的响应速度

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
server {
listen 8886 ;
server_name [你的服务器ip地址];

# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;

location / {
root /html/;
index index.html;
}

#拦截静态资源,static里面存放的我们图片什么的静态资源
location ~ .*\.(gif|jpg|jpeg|bmp|png|ico|js|css)$ {
root /data/;
}

error_page 404 /404.html;
location = /40x.html {
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}

参考文章:
写给后端的Nginx初级入门教程:实战篇
写给后端的Nginx初级入门教程:基础篇

FROM :blog.cfyqy.com | Author:cfyqy

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2022年1月6日01:41:37
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   Nginx浅识http://cn-sec.com/archives/722251.html

发表评论

匿名网友 填写信息