文章目录

一、背景

  • 由于国内巨大的互联网用户群体,既要求Web服务器能在大并发压力下提供稳定的服务,同时又要提供越来越好的用户体验。
  • Nginx先天的事件驱动型设计、全异步的网络I/O处理机制、极少的进程间切换以及许多的优化,都使得Nginx天生善于处理高并发压力下的互联网请求。

初识Nginx一:配置一个静态服务器_nginx

二、安装

  • 操作系统 Linux 2.6以上版本
> uname
  • 通过yum命令进行安装
> yum install nginx
--> Running transaction check
---> Package nginx.x86_64 1:1.16.1-3.el7 will be installed
--> Processing Dependency: nginx-all-modules = 1:1.16.1-3.el7 for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: nginx-filesystem = 1:1.16.1-3.el7 for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_0)(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libssl.so.1.1(OPENSSL_1_1_1)(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: nginx-filesystem for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libcrypto.so.1.1()(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libprofiler.so.0()(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
--> Processing Dependency: libssl.so.1.1()(64bit) for package: 1:nginx-1.16.1-3.el7.x86_64
...
--> Finished Dependency Resolution
Is this ok [y/d/N]: y
Downloading packages:
...
Total 2.6 MB/s | 2.5 MB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : gperftools-libs-2.6.1-1.el7.x86_64 1/12
Installing : 1:nginx-filesystem-1.16.1-3.el7.noarch 2/12
Installing : 1:openssl11-libs-1.1.1g-2.el7.x86_64 3/12
Installing : libXpm-3.5.12-1.el7.x86_64 4/12
Installing : gd-2.0.35-27.el7_9.x86_64
...
Verifying : 1:nginx-mod-http-image-filter-1.16.1-3.el7.x86_64 11/12
Verifying : 1:nginx-mod-stream-1.16.1-3.el7.x86_64 12/12

Installed:
nginx.x86_64 1:1.16.1-3.el7

Dependency Installed:
gd.x86_64 0:2.0.35-27.el7_9 gperftools-libs.x86_64 0:2.6.1-1.el7 libXpm.x86_64 0:3.5.12-1.el7 nginx-all-modules.noarch 1:1.16.1-3.el7
nginx-filesystem.noarch 1:1.16.1-3.el7 nginx-mod-http-image-filter.x86_64 1:1.16.1-3.el7 nginx-mod-http-perl.x86_64 1:1.16.1-3.el7 nginx-mod-http-xslt-filter.x86_64 1:1.16.1-3.el7
nginx-mod-mail.x86_64 1:1.16.1-3.el7 nginx-mod-stream.x86_64 1:1.16.1-3.el7 openssl11-libs.x86_64 1:1.1.1g-2.el7

Complete!
  • 查看nginx是否安装成功;
# 查看nginx安装版本
> nginx -v
nginx version: nginx/1.16.1
# 启动nginx ,默认配置文件为/etc/nginx/nginx.conf
> nginx
> ps aux|grep nginx
root 24834 0.0 0.0 120908 2332 ? Ss 2020 0:00 nginx: master process /usr/sbin/nginx
nginx 24835 0.0 0.0 123992 5768 ? S 2020 0:01 nginx: worker process
nginx 24836 0.0 0.0 123848 5772 ? S 2020 0:02 nginx: worker process
nginx 24837 0.0 0.0 124116 6036 ? S 2020 0:11 nginx: worker process
nginx 24838 0.0 0.0 123996 5776 ? S 2020 0:23 nginx: worker process
...
# 关闭nginx
>

文件或目录

备注

/usr/sbin/nginx

安装后的二进制主文件

/etc/nginx

配置目录

/var/log/nginx

日志目录

三、配置一个静态服务器

3.1 通用格式

  • Nginx的配置文件一个文本文件;
  • events、http、server、location、upstream等都是块配置项:
  1. 块配置项由一个块配置项名和一对大括号组成;
  2. 块配置项可以嵌套;
  3. 每行配置的结尾需要加上分号.;
  4. 可以加“#”注释掉这一行配置;
  5. 在配置项中可以使用变量;
  6. 配置项中的空间单位可以为KB或者MB;
  7. 配置项中的时间单位可以为ms(毫秒),s(秒),m(分钟),h(小时),d(天),w(周,包含7天),M(月,包含30天),y(年,包含365天);

3.2 配置举例

# Nginx worker进程运行的用户及用户组
user nginx;
# Nginx worker进程个数
worker_processes auto;
# error日志的设置
error_log /var/log/nginx/error.log;
# pid文件的路径
pid /run/nginx.pid;

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

# 事件类配置项
events {
# 每个worker的最大连接数
worker_connections 1024;
}

# 配置一个静态Web服务器
http {
# 日志格式设置
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选项:启用Linux上的sendfile系统调用来发送文件,它减少了内核态与用户态之间的两次内存复制
sendfile on;
tcp_nopush on;
tcp_nodelay on;
# keepalive超时时间:闲置超过一定时间后,关闭这个连接
keepalive_timeout 65;

types_hash_max_size 2048;
# HTTP请求包体的最大值
client_max_body_size 50m;
# 定义MIME类型和后缀名关联的文件的位置
include /etc/nginx/mime.types;
# 指定mime.types文件中没有的后缀名的处理方法
default_type application/octet-stream;

# websocket:根据变量 $http_upgrade 的值创建新的变量 $connection_upgrade
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
# 监听端口
listen 80 default_server;
listen [::]:80 default_server;
#主机名称
server_name _;
# 设置资源路径
root /usr/share/nginx/html;

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

# 匹配所有请求
location / {
}

# 根据HTTP返回码重定向页面
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

3.3 运行测试

  • 打开浏览器访问服务器80端口,浏览器打开了如下页面:
  • 初识Nginx一:配置一个静态服务器_safari_02


  • 初识Nginx一:配置一个静态服务器_html_03

  • 打开浏览器访问服务器地址的index2.html,浏览器打开了如下页面:
  • 初识Nginx一:配置一个静态服务器_html_04


  • 初识Nginx一:配置一个静态服务器_safari_05

  • 查看日志
> tail -f /var/log/nginx/access.log
192.168.0.10 - - [01/Feb/2021:13:14:44 +0800] "GET /img/centos-logo.png HTTP/1.1" 200 3030 "http://192.168.0.1/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:14:44 +0800] "GET /img/html-background.png HTTP/1.1" 200 1801 "http://192.168.0.1/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:14:44 +0800] "GET /img/header-background.png HTTP/1.1" 200 82896 "http://192.168.0.1/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:14:45 +0800] "GET /favicon.ico HTTP/1.1" 404 3650 "http://192.168.0.1/" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:16:29 +0800] "GET /1 HTTP/1.1" 404 3650 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:16:29 +0800] "GET /nginx-logo.png HTTP/1.1" 200 368 "http://192.168.0.1/1" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:16:29 +0800] "GET /poweredby.png HTTP/1.1" 200 368 "http://192.168.0.1/1" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"
192.168.0.10 - - [01/Feb/2021:13:44:33 +0800] "GET /index2.html HTTP/1.1" 404 3650 "-" "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36" "-"