侧边栏壁纸
博主头像
王旭阳个人博客博主等级

工欲善其事,必先利其器

  • 累计撰写 111 篇文章
  • 累计创建 28 个标签
  • 累计收到 22 条评论

目 录CONTENT

文章目录

Nginx 配置禁止 IP 直接访问

wxy
wxy
2022-06-03 / 0 评论 / 7 点赞 / 630 阅读 / 3865 字
温馨提示:
本文最后更新于 2023-08-31,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。

不是使用Oneinstack直接跳过此步骤

我服务器用的OneinStack脚本安装的Nginx
默认路径是
/usr/local/nginx/conf/
默认访问ip是OneinStack页面 找到如下配置块

######################## default ############################
  server {
    listen 80;
    server_name _;
    access_log /data/wwwlogs/access_nginx.log combined;
    root /data/wwwroot/default;
    index index.html index.htm index.php;
    #error_page 404 /404.html;
    #error_page 502 /502.html;
    location /nginx_status {
      stub_status on;
      access_log off;
      allow 127.0.0.1;
      deny all;
    }
    location ~ [^/]\.php(/|$) {
      #fastcgi_pass remote_php_ip:9000;
      fastcgi_pass unix:/dev/shm/php-cgi.sock;
      fastcgi_index index.php;
      include fastcgi.conf;
    }
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|mp4|ico)$ {
      expires 30d;
      access_log off;
    }
    location ~ .*\.(js|css)?$ {
      expires 7d;
      access_log off;
    }
    location ~ ^/(\.user.ini|\.ht|\.git|\.svn|\.project|LICENSE|README.md) {
      deny all;
    }
    location /.well-known {
      allow all;
    }
  }

把上面代码 注释或者删掉;

修改自己的nginx.conf文件
替换掉默认的 80 server,如果没有默认的就直接使用下面配置;

方案一

server
{
    listen 80;
    server_name _;
    return 403;
}

方案二

把这些流量收集起来,导入到自己的网站,只要做以下跳转设置:

server
{
    listen 80;    
    server_name _;
    rewrite ^(.*) http://自己的网址 permanent; 
}

https流量也拦截
这里的证书,可以使用你要跳转的网站的证书,或者其他证书都行

server
{
    listen 80;
    listen 443 ssl http2;

    # 这里的证书,可以使用你要跳转的网站的证书,或者其他证书都行
    ssl_certificate    /usr/local/nginx/conf/ssl/www.wxy97.com.pem;
    ssl_certificate_key  /usr/local/nginx/conf/ssl/www.wxy97.com.key;
    ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
    
    server_name _;
    rewrite ^(.*) http://www.ydyno.com permanent; 
}

重载配置

nginx -t #检查配置

nginx -s reload 重载配置

修改配置文件之后需要重载(或重启nginx服务)后生效

7

评论区