Nginx 是由伊戈尔·赛索耶夫为 俄罗斯 访问量第二的 Rambler.ru 站点开发的一款 高性能 的 HTTP 和 反向代理 web服务器
注:本技术应用广泛,技术原理合法合规。中国大陆使用 nginx 的网站有:百度、京东、新浪、网易、腾讯、淘宝等。
- 正向代理和反向代理:
正向代理(来自百度百科):
为了从目标服务器中取得内容,客户端向代理服务器发送一个请求并指定目标服务器,然后代理向目标服务器转交请求并将获得的内容返回给客户端。
反向代理(来自百度百科):
对于用户而言,反向代理服务器就相当于目标服务器,用户不需要知道目标网站的地址,也无须在用户端作任何设定,用户只需直接访问反向代理服务器就可以获得目标服务器的资源。
- 负载均衡:
- 动静分离
为了加快网站的解析速度,可以将动态页面和静态页面由不同的服务器来解析,加快解析速度以降低单个服务器的压力
- Nginx 的下载安装
Step1. Nginx 官网下载地址:http://nginx.org/en/download.html
Step2. 下载安装 pcre(nginx需要的依赖):https://ftp.pcre.org/pub/pcre/
将下载好的 pcre 包上传至 /usr/src 目录下并解压:
tar -xvf pcre-8.44.tar.gz
进入解压之后的目录,执行以下命令:
./configure
make && make install
安装好之后,使用以下命令检查版本号:
[root@izbp10tup89om84qulgxbsz pcre-8.44]# pcre-config --version
8.44
Step3. 安装其他的依赖
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
Step4. 安装Nginx
将下载好的 nginx 包上传至 /usr/src 目录下并解压:
tar -xvf nginx-1.18.0.tar.gz
进入解压之后的目录,执行以下命令:
./configure
make && make install
安装完成后,进入 /usr/local 目录,发现多了一个 nginx 目录:
启动 Nginx:
进入 /usr/local/nginx/sbin 目录,执行以下命令:
./nginx
检查进程:
[root@izbp10tup89om84qulgxbsz sbin]# ps -ef | grep nginx
root 10237 1 0 17:00 ? 00:00:00 nginx: master process ./nginx
nobody 10238 10237 0 17:00 ? 00:00:00 nginx: worker process
root 10240 2239 0 17:00 pts/0 00:00:00 grep --color=auto nginx
访问(服务器需开放80端口):
- Nginx 操作的常用命令:
注:使用 nginx 的操作命令必须进入 nginx 的目录:/usr/local/nginx/sbin
查看 nginx 的版本号:
[root@izbp10tup89om84qulgxbsz sbin]# ./nginx -v
nginx version: nginx/1.18.0
查看 nginx 相关进程(查看是否启动,以下是关闭状态):
[root@izbp10tup89om84qulgxbsz sbin]# ps -ef | grep nginx
root 10868 10844 0 22:38 pts/0 00:00:00 grep --color=auto nginx
启动 nginx:
[root@izbp10tup89om84qulgxbsz sbin]# ./nginx
关闭 nginx:
[root@izbp10tup89om84qulgxbsz sbin]# ./nginx -s stop
重新加载 nginx(当修改了 nginx 的配置文件后,不需要重启 nginx ,只需要重新加载即可):
[root@izbp10tup89om84qulgxbsz sbin]# ./nginx -s reload
- Nginx 的配置文件:
注:Nginx 的配置文件 nginx.conf 所在的目录:/usr/local/nginx/conf
Nginx 配置文件可分为以下三部分:
- 全局块
从配置文件开始到 events 块之间的内容,主要会设置一些影响 nginx 服务器整体运行的配置指令:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
worker_processes 值越大,可支持的并发处理量就越大
- events 块
主要设置 Nginx 服务器与用户的网络连接:
events {
worker_connections 1024;
}
worker_connections 指定支持的最大连接数
- http 块
配置最频繁的部分,其中又包含 http 全局块 与 server 块
http全局块中主要配置文件引入、MIME-TYPE 定义、日志自定义、连接超时时间、单链接请求数上限等
server 块中又包含全局 server 块和 location 块
http {
include 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 logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}