Nginx入门指南
Nginx介绍
Nginx [engine x] 是一个高性能的 HTTP 和反向代理服务器、邮件代理服务器和通用 TCP/UDP 代理服务器,最初由Igor Sysoev编写。
很长一段时间以来,它一直在运行在许多负载重的俄罗斯网站,典型代表包括 Yandex、 Mail.Ru、 VK和 Rambler。
根据 Netcraft 的数据,2022 年 1 月,Nginx 服务或代理了最繁忙的站点中的 22.16% 。其中的成功案例包括 Dropbox、 Netflix、 Wordpress.com、 FastMail.FM。
Nginx 特点是占用内存少,并发能力强,事实上 Nginx 的并发能力确实在同类型的网页服务器中表现较好。Nginx 专为性能优化而开发,性能是其最重要的要求,Nginx 十分注重效率,有报告显示 Nginx 能支持高达 50000 个并发连接数。
Nginx安装
Nginx 官方下载地址:
http://nginx.org/en/download.html
系统平台:CentOS Linux release 7.6.1810 (Core)
安装编译工具及库文件
yum -y install gcc-c++ libtool make openssl openssl-devel pcre pcre-devel zlib zlib-devel
[root@VM-8-12-centos ~]# yum -y install gcc-c++ libtool make openssl openssl-devel pcre pcre-devel zlib zlib-devel
Loaded plugins: fastestmirror, langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
Loading mirror speeds from cached hostfile
Package gcc-c++-4.8.5-44.el7.x86_64 already installed and latest version
Package libtool-2.4.2-22.el7_3.x86_64 already installed and latest version
Package 1:make-3.82-24.el7.x86_64 already installed and latest version
Package 1:openssl-1.0.2k-24.el7_9.x86_64 already installed and latest version
Package 1:openssl-devel-1.0.2k-24.el7_9.x86_64 already installed and latest version
Package pcre-8.32-17.el7.x86_64 already installed and latest version
Package pcre-devel-8.32-17.el7.x86_64 already installed and latest version
Package zlib-1.2.7-19.el7_9.x86_64 already installed and latest version
Package zlib-devel-1.2.7-19.el7_9.x86_64 already installed and latest version
Nothing to do
安装 PCRE
PCRE 作用是让 Nginx 支持 Rewrite 功能。
下载 PCRE 安装包,下载地址:
https://onboardcloud.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
[root@VM-8-12-centos tmp]# wget https://onboardcloud.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
--2022-03-07 21:51:26-- https://onboardcloud.dl.sourceforge.net/project/pcre/pcre/8.45/pcre-8.45.tar.gz
Resolving onboardcloud.dl.sourceforge.net (onboardcloud.dl.sourceforge.net)... 202.79.184.253
Connecting to onboardcloud.dl.sourceforge.net (onboardcloud.dl.sourceforge.net)|202.79.184.253|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 2096552 (2.0M) [application/x-gzip]
Saving to: ‘pcre-8.45.tar.gz’
100%[===============================================================================>] 2,096,552 20.1KB/s in 1m 52s
2022-03-07 21:53:20 (18.3 KB/s) - ‘pcre-8.45.tar.gz’ saved [2096552/2096552]
解压安装包
[root@VM-8-12-centos tmp]# tar zxvf pcre-8.45.tar.gz
进入安装包目录
[root@VM-8-12-centos tmp]# cd pcre-8.45/
编译安装
[root@VM-8-12-centos pcre-8.45]# ./configure
[root@VM-8-12-centos pcre-8.45]# make && make install
查看pcre版本
[root@VM-8-12-centos pcre-8.45]# pcre-config --version
8.45
安装 Nginx
下载 Nginx,下载地址:
http://nginx.org/en/download.html
http://nginx.org/download/nginx-1.21.6.tar.gz
[root@VM-8-12-centos pcre-8.45]# cd ~/tmp
[root@VM-8-12-centos tmp]# wget http://nginx.org/download/nginx-1.21.6.tar.gz
解压安装包
[root@VM-8-12-centos tmp]# tar zxvf nginx-1.21.6.tar.gz
进入安装包目录
[root@VM-8-12-centos tmp]# cd nginx-1.21.6/
编译安装
[root@VM-8-12-centos nginx-1.21.6]# ./configure --prefix=/root/tmp/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/root/tmp/pcre-8.45
[root@VM-8-12-centos nginx-1.21.6]# make
[root@VM-8-12-centos nginx-1.21.6]# make install
Configuration summary
+ using PCRE library: /root/tmp/pcre-8.45
+ using system OpenSSL library
+ using system zlib library
nginx path prefix: "/root/tmp/webserver/nginx"
nginx binary file: "/root/tmp/webserver/nginx/sbin/nginx"
nginx modules path: "/root/tmp/webserver/nginx/modules"
nginx configuration prefix: "/root/tmp/webserver/nginx/conf"
nginx configuration file: "/root/tmp/webserver/nginx/conf/nginx.conf"
nginx pid file: "/root/tmp/webserver/nginx/logs/nginx.pid"
nginx error log file: "/root/tmp/webserver/nginx/logs/error.log"
nginx http access log file: "/root/tmp/webserver/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"
查看nginx版本
[root@VM-8-12-centos nginx-1.21.6]# /root/tmp/webserver/nginx/sbin/nginx -v
nginx version: nginx/1.21.6
到此,nginx安装完成。
nginx.conf
默认配置
[root@VM-8-12-centos conf]# cat nginx.conf
#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;
events {
worker_connections 1024;
}
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;
# }
#}
}
检查配置文件nginx.conf的正确性
[root@VM-8-12-centos ~]# /root/tmp/webserver/nginx/sbin/nginx -t
nginx: the configuration file /root/tmp/webserver/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /root/tmp/webserver/nginx/conf/nginx.conf test is successful
启动 Nginx
[root@VM-8-12-centos ~]# /root/tmp/webserver/nginx/sbin/nginx
Nginx 常用命令
/root/tmp/webserver/nginx/sbin/nginx -s reload # 重新载入配置文件
/root/tmp/webserver/nginx/sbin/nginx -s reopen # 重启 Nginx
/root/tmp/webserver/nginx/sbin/nginx -s stop # 停止 Nginx
/root/tmp/webserver/nginx/sbin/nginx -s quit # 停止 Nginx(推荐)
Nginx 报错 403 Forbidden
我是在在本地用虚拟机中通过yum安装nginx的,安装一切正常,但是访问时报错403 Forbidden
于是查看nginx日志,路径为/root/tmp/webserver/nginx/logs/error.log
。打开日志发现报错Permission denied,详细报错如下:
[root@VM-8-12-centos sbin]# cat /root/tmp/webserver/nginx/logs/error.log
2022/03/07 22:14:41 [error] 19951#0: *1 "/root/tmp/webserver/nginx/html/index.html" is forbidden (13: Permission denied), client: 103.142.140.81, server: localhost, request: "GET / HTTP/1.1", host: "82.156.13.32"
启动用户和nginx工作用户不一致
查看nginx的启动用户,发现是nobody,而为是用root启动的
[root@VM-8-12-centos local]# ps aux | grep "nginx: worker process" | awk '{print $1}'
root
nobody
root
将nginx.config的user改为和启动用户一致
[root@VM-8-12-centos conf]# vim nginx.conf
[root@VM-8-12-centos conf]# cat nginx.conf
user root;
worker_processes 1;
缺少index.html或者index.php文件
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
如果在/root/tmp/webserver/nginx/html
下面没有index.php,index.html的时候,直接文件,会报403 forbidden。
权限问题
修改web目录的读写权限,或者是把nginx的启动用户改成目录的所属用户,重启Nginx即可解决
[root@VM-8-12-centos nginx]# chmod 777 -R html/
SELinux设置为开启状态(enabled)
查看当前selinux的状态
[root@VM-8-12-centos nginx]# /usr/sbin/sestatus
将SELINUX=enforcing 修改为 SELINUX=disabled 状态
[root@VM-8-12-centos ~]# vi /etc/selinux/config
#SELINUX=enforcing
SELINUX=disabled
重启生效。
自定义网站跟目录
解决方案
[root@VM-8-12-centos ~]# cat /root/tmp/webserver/nginx/conf/nginx.conf
user root; # 以root用户运行nginx
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
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 /root/tmp/webserver/nginx/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;
# }
#}
}
按照上述配置文件配置完成之后重启nginx服务
[root@VM-8-12-centos sbin]# ./nginx -s reload
完美解决。
访问网站http://82.156.13.32/
反向代理
代理
代理也被叫做网络代理,是一种比较特殊的网络服务,允许一个终端(通常指客户端)通过这个服务与另一个终端(通常指服务器端)进行非直接的连接。
例如:一些网关、路由器等网络设备都具备网络代理的功能。
代理服务有利于保障网络终端的隐私或者安全,可以在一定程度上阻止网络攻击(因为通过代理,可以隐藏真正的服务器端/客户端)。
代理服务器
左边和右边的电脑在通讯时候,需要经过中间的电脑中转,而中间的那部电脑就是代理服务器。
代理请求过程
客户端首先根据代理服务器所使用的代理协议,与代理服务器创建连接,接着按照协议请求对目标服务器创建连接或者获得目标服务器的指定资源(如:文件)。
正向代理
通常我们说的代理,都是指的正向代理。
继续看这张图,你会发现,此处的代理服务器可以由客户端提供,也可以由服务器端提供。
当客户端主动使用代理服务器时,此时的代理叫正向代理。比如:一些网络代理工具(加速器/VPN)
正向代理:
局域网中的电脑用户想要直接访问网络是不可行的,只能通过代理服务器来访问,这种代理服务就被称为正向代理。比如FQ软件。
正向代理时,由客户端发送对某一个目标服务器的请求,代理服务器在中间将请求转发给该目标服务器,目标服务器将结果返回给代理服务器,代理服务器再将结果返回给客户端。
使用正向代理时,客户端是需要配置代理服务的地址、端口、账号密码(如有)等才可使用的。
通过上图可以看到,客户端并没有直接与服务器相连。正向代理隐藏了真实的客户端地址。可以很好地保护客户端的安全性。
正向代理的适用场景
•访问被禁止的资源(让客户端访问原本不能访问的服务器。可能是由于路由的原因,或者策略配置的原因,客户端不能直接访问某些服务器。为了访问这些服务器,可通过代理服务器来访问)•突破网络审查(比如谷歌、youtube…)•客户端IP被服务器封禁,可以绕过IP封禁•突破网站的区域限制•隐藏客户端的地址(对于被请求的服务器而言,代理服务器代表了客户端,所以在服务器或者网络拓扑上,看不到原始客户端)•进行客户访问控制•可以集中部署策略,控制客户端的访问行为(访问认证等)•记录用户访问记录(上网行为管理)•内部资源的控制(公司、教育网等)•加速访问资源•使用缓冲特性减少网络使用率(代理服务器设置一个较大的缓冲区,当有外界的信息通过时,同时也将其保存到缓冲区中,当其他用户再访问相同的信息时,则直接由缓冲区中取出信息,传给用户,以提高访问速度。)•过滤内容(可以通过代理服务器统一过滤一些危险的指令/统一加密一些内容、防御代理服务器两端的一些攻击性行为)
反向代理
服务器根据客户端的请求,从其关系的一组或多组后端服务器(如Web服务器)上获取资源,然后再将这些资源返回给客户端,客户端只会得知代理服务器的IP地址,而不知道在代理服务器后面的服务器集群的存在。
反向代理整个流程:
由客户端发起对代理服务器的请求,代理服务器在中间将请求转发给某一个服务器,服务器将结果返回给代理服务器,代理服务器再将结果返回给客户端。
反向代理:
客户端无法感知代理,因为客户端访问网络不需要配置,只要把请求发送到反向代理服务器,由反向代理服务器去选择目标服务器获取数据,然后再返回到客户端。
此时反向代理服务器和目标服务器对外就是一个服务器,暴露的是代理服务器地址,隐藏了真实服务器 IP 地址。
Nginx在做反向代理时,提供性能稳定,并且能够提供配置灵活的转发功能。Nginx可以根据不同的正则匹配,采取不同的转发策略,比如图片文件结尾的走文件服务器,动态页面走web服务器,只要你正则写的没问题,又有相对应的服务器解决方案,你就可以随心所欲的玩。并且Nginx对返回结果进行错误页跳转,异常判断等。如果被分发的服务器存在异常,他可以将请求重新转发给另外一台服务器,然后自动去除异常服务器。
反向代理的适用场景
•负载均衡•如果服务器集群中有负荷较高者,反向代理通过URL重写,根据连线请求从负荷较低者获取与所需相同的资源或备援。可以有效降低服务器压力,增加服务器稳定性•提升服务器安全性•可以对客户端隐藏服务器的IP地址•作为应用层防火墙,为网站提供对基于Web的攻击行为(例如DoS/DDoS)的防护,更容易排查恶意软件等•加密/SSL加速:将SSL加密工作交由配备了SSL硬件加速器的反向代理来完成•提供缓存服务,加速客户端访问•对于静态内容及短时间内有大量访问请求的动态内容提供缓存服务•数据统一压缩•节约带宽•为网络带宽不好的网络提供服务•统一的访问权限控制•统一的访问控制•突破互联网的封锁•突破谷歌访问封锁
正向代理与反向代理的区别
最核心的不同在于代理的对象不同。
•正向代理是代理客户端。•反向代理是代理服务器。
代理哪端便可以隐藏哪端。
也就是说:
•正向代理隐藏真实客户端•反向代理隐藏真实服务端
反向代理为什么叫反向代理
从我们用户的角度来看:
代理我们发出请求的客户端被称为正向代理。
而代理我们访问的服务器,则被称为反向代理。
从代理结构的角度来看(代理服务器在两种代理中的作用均为收发请求与响应):
客户端与代理服务器属于一个局域网(看图左边),称为正向代理。
服务器端与代理服务器属于一个局域网时(看图右边),称为反向代理。
•正向代理为客户端服务。•反向代理为服务器端服务。
原文始发于微信公众号(利刃信安):Nginx入门指南
- 左青龙
- 微信扫一扫
-
- 右白虎
- 微信扫一扫
-
评论