LOGO 首页 OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 技术文档 其他文档  
 
网站管理员

Nginx入门基础知识

admin
2025年12月4日 1:58 本文热度 404

#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配置文件

隐藏版本号


http {    server_tokens off;}
经常会有针对某个版本的nginx安全漏洞出现,隐藏nginx版本号就成了主要的安全优化手段之一,当然最重要的是及时升级修复漏洞

开启HTTPS


server {    listen 443;    server_name ops-coffee.com;    ssl on;    ssl_certificate /etc/nginx/server.crt;    ssl_certificate_key /etc/nginx/server.key;    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;    ssl_ciphers         HIGH:!aNULL:!MD5;}

ssl on: 开启https

ssl_certificate: 配置nginx ssl证书的路径

ssl_certificate_key: 配置nginx ssl证书key的路径

ssl_protocols: 指定客户端建立连接时使用的ssl协议版本,如果不需要兼容TSLv1,直接去掉即可

ssl_ciphers: 指定客户端连接时所使用的加密算法,你可以再这里配置更高安全的算法



添加黑白名单


白名单配置

location /admin/ {    allow   192.168.1.0/24;    deny    all;}

上边表示只允许192.168.1.0/24网段的主机访问,拒绝其他所有

也可以写成黑名单的方式禁止某些地址访问,允许其他所有,例如

location /ops-coffee/ {    deny   192.168.1.0/24;    allow    all;}

更多的时候客户端请求会经过层层代理,我们需要通过$http_x_forwarded_for来进行限制,可以这样写

set $allow false;if ($http_x_forwarded_for = "211.144.204.2") { set $allow true; }if ($http_x_forwarded_for ~ "108.2.66.[89]") { set $allow true; }if ($allow = false) { return 404; }
添加账号认证


server {    location / {        auth_basic "please input user&passwd";        auth_basic_user_file key/auth.key;    }}
限制请求方法


if ($request_method !~ ^(GET|POST)$ ) {    return 405;}
$request_method能够获取到请求nginx的method 
配置只允许GET\POST方法访问,其他的method返回405

拒绝User-Agent


if ($http_user_agent ~* LWP::Simple|BBBike|wget|curl) {    return 444;}

可能有一些不法者会利用wget/curl等工具扫描我们的网站,我们可以通过禁止相应的user-agent来简单的防范

Nginx的444状态比较特殊,如果返回444那么客户端将不会收到服务端返回的信息,就像是网站无法连接一样



图片防盗链


location /images/ {    valid_referers none blocked www.ops-coffee.com ops-coffee.com;    if ($invalid_referer) {        return  403;    }}

valid_referers: 验证referer,其中none允许referer为空,blocked允许不带协议的请求,除了以上两类外仅允许referer为www.ops-coffee.com或ops-coffee.com时访问images下的图片资源,否则返回403

当然你也可以给不符合referer规则的请求重定向到一个默认的图片,比如下边这样

location /images/ {    valid_referers blocked www.ops-coffee.com ops-coffee.com    if ($invalid_referer) {        rewrite ^/images/.*\.(gif|jpg|jpeg|png)$ /static/qrcode.jpg last;    }}
控制并发连接数


可以通过ngx_http_limit_conn_module模块限制一个IP的并发连接数
http {    limit_conn_zone $binary_remote_addr zone=ops:10m;    server {        listen       80;        server_name  ops-coffee.com;        root /home/project/webapp;        index index.html;        location / {            limit_conn ops 10;        }        access_log  /tmp/nginx_access.log  main;    }}

limit_conn_zone: 设定保存各个键(例如$binary_remote_addr)状态的共享内存空间的参数,zone=空间名字:大小

大小的计算与变量有关,例如$binary_remote_addr变量的大小对于记录IPV4地址是固定的4 bytes,而记录IPV6地址时固定的16 bytes,存储状态在32位平台中占用32或者64 bytes,在64位平台中占用64 bytes。1m的共享内存空间可以保存大约3.2万个32位的状态,1.6万个64位的状态

limit_conn: 指定一块已经设定的共享内存空间(例如name为ops的空间),以及每个给定键值的最大连接数

上边的例子表示同一IP同一时间只允许10个连接

当有多个limit_conn指令被配置时,所有的连接数限制都会生效

http {    limit_conn_zone $binary_remote_addr zone=ops:10m;    limit_conn_zone $server_name zone=coffee:10m;    server {        listen       80;        server_name  ops-coffee.com;        root /home/project/webapp;        index index.html;        location / {            limit_conn ops 10;            limit_conn coffee 2000;        }    }}
上边的配置不仅会限制单一IP来源的连接数为10,同时也会限制单一虚拟服务器的总连接数为2000。


阅读原文:原文链接


该文章在 2026/12/3 10:35:54 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2026 ClickSun All Rights Reserved  粤ICP备13012886号-2  粤公网安备44030602007207号