时间:2022-02-09来源:www.pcxitongcheng.com作者:电脑系统城
| 1 | ssh 用户名@公网ip |
默认的用户名是root,假如公网 ip 是 a.b.c.d, 那链接命名就是
| 1 | ssh root@a.b.c.d |
| 1 2 |
yum -y install gcc gcc-c++ autoconf pcre pcre-devel make automakeyum -y install wget httpd-tools vim |
查看iptables规则
| 1 2 3 |
iptables -L或iptables -t nat -L |
关闭 iptables 规则
| 1 2 3 |
iptables -F或iptables -t nat -F |
查看是否打开
| 1 | getenforce |
关闭
| 1 | setenforce 0 |
Nginx 是一个开源且高性能、高可靠的 HTTP 中间件、代理服务。
To set up the yum repository for RHEL/CentOS, create the file named /etc/yum.repos.d/nginx.repo with the following contents:
1
2
3
4
5[nginx]name=nginx repobaseurl=http://nginx.org/packages/OS/OSRELEASE/$basearch/gpgcheck=0enabled=1
Replace “OS” with “rhel” or “centos”, depending on the distribution used, and “OSRELEASE” with “6” or “7”, for 6.x or 7.x versions, respectively.
查看nginx的所有安装目录
| 1 | rpm -ql nginx |
然后得到如下配置
| 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 |
[root@ ~]# rpm -ql nginxnginx日志轮转,用于logrotate服务的日志切割/etc/logrotate.d/nginxnginx主配置文件/etc/nginx/nginx.conf/etc/nginx/etc/nginx/conf.d/etc/nginx/conf.d/default.confcgi配置相关,fastcgi配置/etc/nginx/fastcgi_params/etc/nginx/scgi_params/etc/nginx/uwsgi_params编码转换映射转化文件/etc/nginx/koi-utf/etc/nginx/koi-win/etc/nginx/win-utf设置http协议的 Content-Type 与扩展名对应关系/etc/nginx/mime.types用于配置出系统守护进程管理器管理方式/etc/sysconfig/nginx/etc/sysconfig/nginx-debug/usr/lib/systemd/system/nginx-debug.service/usr/lib/systemd/system/nginx.servicenginx模块目录/etc/nginx/modules/usr/lib64/nginx/modules/usr/lib64/nginx/usr/libexec/initscripts/legacy-actions/nginx/usr/libexec/initscripts/legacy-actions/nginx/check-reload/usr/libexec/initscripts/legacy-actions/nginx/upgradenginx服务的启动管理的终端命令/usr/sbin/nginx/usr/sbin/nginx-debugnginx的手册和帮助文件/usr/share/doc/nginx-1.14.0/usr/share/doc/nginx-1.14.0/COPYRIGHT/usr/share/man/man8/nginx.8.gz/usr/share/nginx/usr/share/nginx/html/usr/share/nginx/html/50x.html/usr/share/nginx/html/index.htmlnginx 的缓存目录/var/cache/nginxnginx日志目录/var/log/nginx |
命令 nginx -V 查看所有编译参数
| 参数 | 说明 |
|---|---|
| user | 设置nginx服务的系统使用用户 |
| worker_processes | 工作进程数(一般与服务器核数保持一致) |
| rror_log | nginx的错误日志 |
| pid | nginx服务启动时候pid |
| events -> worker_connections | 每个进程允许最大连接数 |
| events -> use | 工作进程数 |
文件路径 /etc/nginx/conf.d/default.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 |
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/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 /usr/share/nginx/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; #}} |
可以去 /usr/share/nginx/html/index.html 修改默认的展示页面,也可以去 /usr/share/nginx/html/50x.html 修改错误页面。
修改后重启 nginx
| 1 2 3 |
systemctl reload nginx.service或systemctl restart nginx.service |
检查 nginx 配置,结果出现 successful 表示成功
| 1 | nginx -t -c /etc/nginx/nginx.conf |
重新加载配置
| 1 | nginx -s reload -c /etc/nginx/nginx.conf |


| 1 2 3 |
Syntax: sendfile on|off;Default: sendfile off;Context: http,server,location,if in location |
引读:--with-file-aio 异步文件读取
| 1 2 3 |
Syntax: tcp_nopush on|off;Default: tcp_nopush off;Context: http,server,location |
| 1 2 3 |
Syntax: tcp_nodelay on|off;Default: tcp_nodelay on;Context: http,server,location |
| 1 2 3 |
Syntax: gzip_comp_level level;Default: gzip_comp_level 1;Context: http,server,location |
| 1 2 3 |
Syntax: gzip_http_version 1.0|1.1;Default: gzip_http_version 1.1;Context: http,server,location |
| 1 2 3 4 5 |
预读 gzip 功能http_gzip_static_module应用支持 gunzip 的压缩方式http_gunzip_module |
配置语法 - expires
添加 Cache-Control、Expires 头
| 1 2 3 4 |
Syntax:expires [modified] time; expires epoch | max | offDefault: expires off;Context: http, server, location, if in location |
*表示允许所有的网站跨域,为了安全起见可以设置仅需要的网址
| 1 2 3 4 5 |
location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS; root /opt/app/code} |
| 1 2 3 |
Syntax: valid_referers none | blocked | server_names | string...;Default: -Context: server, location, |
正向代理与反向代理的区别在于代理的对象不一样
正向代理代理的对象是客户端
反向代理代理的对象是服务端
配置语法
| 1 2 3 |
Syntax: proxy_pass URLDefault: -Context: location,if in location,limit_except |
URL 一般是以下三种
| 1 2 3 |
http://localhost:8080/uri/https://192.168.1.1:8000/uri/http://unix:/tmp/backend.socket:/uri/; |
这个模块提供一个简单方法来实现在轮询和客户端IP之间的后端服务器负荷平衡。
配置范例:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
resolver 10.0.0.1;upstream dynamic { zone upstream_dynamic 64k; hash $request_uri; #按照url的hash值来分配,同一个url分配到同一个服务器 server backend1.example.com weight=5; server backend2.example.com:8080 fail_timeout=5s slow_start=30s; server 192.0.2.1 max_fails=3; server backend3.example.com resolve; server backend4.example.com service=http resolve; server backup1.example.com:8080 backup; server backup2.example.com:8080 backup;}server { location / { proxy_pass http://dynamic; health_check; }} |
状态解释
| 配置 | 说明 |
|---|---|
| down | 当前的server暂时不参与负载均衡 |
| backup | 预留的备份服务器 |
| max_fails | 允许请求失败的次数 |
| fail_timeout | 经过max_fails 失败后,服务暂停的时间 |
| max_conns | 限制最大的接收的连接数 |
| 配置 | 说明 |
|---|---|
| 轮询 | 按时间顺序逐一分配到不停的后端服务器 |
| 加权轮询 | weight值越大,分配到的访问几率越高 |
| ip_hash | 每个请求按照访问IP的hash结果分配,这样来自同一个ip固定访问一个后端服务器 |
| url_hash | 按照访问的URL的hash结果来分配请求,使每个URL定向到同一个后端服务器 |
| least_conn | 最少连接数,哪个机器连接数少就分发 |
| hash关键数值 | hash自定义的key |
缓存类型分类:客户端缓存,代理缓存,服务端缓存
proxy_cache
| 1 2 3 4 |
Syntax: proxy_cache zone | off;Default: proxy_cache off;Context: http, server, location |
proxy_cache_path
| 1 2 3 |
Syntax: proxy_cache_path path [levels=levels] [use_temp_path=on|off] keys_zone=name:size [inactive=time] [max_size=size] [manager_files=number] [manager_sleep=time] [manager_threshold=time] [loader_files=number] [loader_sleep=time] [loader_threshold=time] [purger=on|off] [purger_files=number] [purger_sleep=time] [purger_threshold=time];Default: —Context: http |
实例
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=cache_zone:10m max_size=10g inactive=60m use_temp_path=off;map $request_method $purge_method { PURGE 1; default 0;}server { ... location / { proxy_pass http://backend; proxy_cache cache_zone; proxy_cache_key $uri; proxy_cache_purge $purge_method; # 当分配的服务器出现50X 错误时分配另一台服务器 proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504 }} |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
upstream java_api{ server 127.0.0.1:8080;}server { ... #匹配到jsp结尾的请求去请求服务器 location ~ \.jsp$ { proxy_pass http://java_api; index index.html index.htm; } #匹配到图片资源返回本地的内容 location ~ \.(jpg|png|gif)$ { expires 1h; gzip on; }} |
作用:实现 url 重写以及重定向
使用场景:
URL 访问跳转,支持开发设计
页面跳转、兼容性支持、展示效果等
SEO优化
维护。后台维护、流量转发等
安全
语法
| 1 2 3 |
Syntax: rewrite regex replacement [flag];Default: —Context: server, location, if |
如果指定的正则表达式与请求URI匹配,则URI将按照*replacement*字符串中的指定进行更改。重写指令按其在配置文件中的出现顺序依次执行。可以使用标志终止指令的进一步处理。如果替换字符串以“http://”、“https://”或“$scheme”开头,则处理将停止,并将重定向返回给客户端。
可选的*flag*参数可以是:
last
停止rewrite检测
停止处理当前的“ngx_http_rewrite_module”指令集,并开始搜索与更改的URI匹配的新位置;
break
停止rewrite检测
停止处理当前的“ngx_http_rewrite_module”指令集,就像处理break指令一样;
redirect
返回302临时重定向,地址栏会显示跳转后的地址
返回带有302代码的临时重定向;当替换字符串不是以“http://”、“https://”或“$scheme”开头时使用;
permanent
返回302永久重定向,地址栏会显示跳转后的地址
返回带有301代码的永久重定向。
完整的重定向URL是根据请求方案($scheme)和 "theserver_name_in_redirect"和 "port_in_redirect"指令形成的。
例子:
| 1 2 3 4 5 6 7 |
server { ... rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 last; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra last; return 403; ...} |
但是,如果这些指令被放在"/download/"的位置,最后一个标志应该被替换为break,否则nginx将进行10次循环并返回500错误。
| 1 2 3 4 5 |
location /download/ { rewrite ^(/download/.*)/media/(.*)\..*$ $1/mp3/$2.mp3 break; rewrite ^(/download/.*)/audio/(.*)\..*$ $1/mp3/$2.ra break; return 403;} |
如果一个*replacement*字符串包含了新的请求参数,那么以前的请求参数就会被附加到它们之后。如果不希望出现这种情况,例如,在替换字符串的末尾加上一个问号,就可以避免追加这些参数。
| 1 | rewrite ^/users/(.*)$ /show?user=$1? last; |
如果一个正则表达式包括"}"或";"字符,整个表达式应该用单引号或双引号括起来。
指定并允许检查请求的链接的真实性以及保护资源免遭未授权的访问
限制链接生效周期
| 1 2 3 4 5 6 7 |
Syntax: secure_link expression;Default: —Context: http, server, locationSyntax: secure_link_md5 expression;Default: —Context: http, server, location |
例子:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
location /s/ { secure_link $arg_md5,$arg_expires; secure_link_md5 "$secure_link_expires$uri$remote_addr secret"; if ($secure_link = "") { return 403; } if ($secure_link = "0") { return 410; } ...} |
基于 IP 地址匹配 MaxMind GeoIP 二进制文件,读取 IP 所在地域信息
安装: yum install nginx-module-geoip
使用场景
| 1 2 3 4 5 6 7 8 9 |
server { listen 443 ssl; server_name www.example.com; ssl_certificate www.example.com.crt; ssl_certificate_key www.example.com.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ...} |
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
server{ listen 443; server_name 116.62.103.228 jeson.t.imooc.io; keepalive_timeout 100; ssl on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; ssl_certificate www.example.com.crt; ssl_certificate_key www.example.com.key; index index.html index.htm; location / { root /opt/app/code; }} |
Lua 是一个简洁、轻量、可扩展的脚本语言
Nginx + Lua 优势:充分的结合 Nginx 的并发处理 epoll 优势和 Lua 的轻量实现简单的功能且高并发的场景。
| 1 | yum install lua |
命令行模式
| 1 | 在命令行输入 lua 开启命令行交互模式 |
脚本模式
编写 test.lua 文件,执行 lua test.lua 运行
| 1 2 3 4 |
-- 行注释--[[ 块注释--]] |
如果多个文件配置有相同的 server_name ,根据文件名先读取到哪个文件就加载哪个文件的配置
| 1 2 3 |
= 进行普通字符精确匹配,也就是完全匹配^~ 表示普通字符匹配,使用前缀匹配~ \~* 表示执行一个正则匹配() |
前两种匹配到之后就不往下继续匹配了,最后一种会继续往下匹配,如果没有匹配到,就用它的匹配。也就是前两种优先级比第三种高。
按顺序检查文件是否存在
| 1 2 3 |
location / { try_files $uri $uri/ /index.php;} |
文件句柄:linux\Unix 一切皆文件,文件句柄就是一个索引
设置方式:系统全局性修改,用户局部性修改,进程局部性修改
修改方法:
| 1 | vim /etc/security/limits.conf |
加入以下代码
| 1 2 3 4 5 6 |
# 给root用户设置root soft nofile 10000root hard nofile 10000# 给所有用户全局设置* soft nofile 10000* hard nofile 10000 |
soft 不是强制性的,超过设置的值会提醒但不强制执行;hard 会强制执行
| 1 | vim /etc/nginx/nginx.conf |
添加以下代码
| 1 | worker_rlimit_nofile 20000 |
查看当前服务器的 CPU 个数
| 1 | cat /proc/cpuinfo | grep "physical id"|sort|uniq|wc -l |
查看 CPU 核数
| 1 | cat /proc/cpuinfo | grep "cpu cores"|uniq |
worker_processes = CPU 个数 * CPU 核数
假如有 2 个 CPU,每个 CPU 有 8 核,那 worker_processes 应该是16
打开 nginx 配置文件 vim /etc/nginx/nginx.conf
| 1 2 |
worker_processes 16;worker_cpu_affinity auto; |
然后刷新nginx配置 nginx -s reload -c /etc/nginx/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 |
user nginx;worker_processes 1;worker_cpu_affinity auto;error_log /var/log/nginx/error.log warn;pid /var/run/nginx.pid;worker_rlimit_nofile 10000;events { use epoll; worker_connections 1024;}http { include /etc/nginx/mime.types; default_type application/octet-stream; charset utf-8; 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; gzip_disable "MSIE [1-6]\."; gzip_http_version 1.1; include /etc/nginx/conf.d/*.conf;} |
常见恶意行为:爬虫行为和恶意抓取、资源盗用
基础防盗链功能:目的不让恶意用户轻易爬取网站数据
secure_link_module: 提高数据安全性,对数据增加加密验证和失效性,适合核心重要数据
access_module: 对后台、部分用户服务的数据提供 IP 防控
通过猜测密码字段不断对后台系统登录性尝试,获取后台登录密码
防范手段:
| 1 2 3 4 5 6 |
location ^~ /upload{ root /opt/app/images; if($requst_filename ~*(.*)\.php){ return 403; }} |
利用未过滤/未审核用户输入的攻击方法,让应用运行本不应该运行的 SQL 代码
Nginx + Lua 防火墙实现:https://github.com/cachecats/coderiver
更多关于nginx的技术文章请查看下面的相关链接
2024-07-07
myeclipse怎么导入tomcat教程2024-07-07
myeclipse如何启动tomcat2024-07-07
myeclipse如何绑定tomcat上线了一个小的预约程序,配置通过Nginx进行访问入口,默认的日志是没有请求时间的,因此需要配置一下,将每一次的请求的访问响应时间记录出来,备查与优化使用....
2023-03-17