设为首页 收藏本站
查看: 559|回复: 0

[经验分享] nginx 配置优化指南-相关内容【四】

[复制链接]

尚未签到

发表于 2018-11-8 12:53:53 | 显示全部楼层 |阅读模式
一、Nginx后台项目限制外网访问
  公司合规要求,后台地址限制外网访问,所以通过匹配进行限制,但通过nginx deny allow进行设置无法生效,故通过用户真实IP进行判断,步骤如下:
  1)环境介绍:
  场景: 前端SLB--->nginx---->proxy
DSC0000.jpg

  2)配置SLB
  开启Xforwardfor 记录用户真实IP
DSC0001.jpg

  SLB其它配置,可去阿里云产品页面查询
  3)nginx修改配置
  添加到server 标签内
  

#-----------------------------IP白名单-----------------------------  set $allow true;
  if ($http_x_forwarded_for ~ "白名单IP1|白名单IP2"){ #如果用户真实IP是后面列出的
  set $allow false;   #那allow状态为false
  }
  if ($allow != false){  #如何状态不为flase 就返回404,也就是说不是授权的用户,就返回404
  return 404;
  }
  

二、阿里云SLB场景使用Nginx封用户真实IP
  1.环境与上面一致
  2.tomcat开启X-Forwarded-For日志功能
  开启tomcat的X-Forwarded-For,在tomcat/conf/server.xml中,修改AccessLogValve日志纪录功能为如下内容:
  

    prefix="localhost_access_log." suffix=".txt"  pattern="%{X-Forwarded-For}i %h %l %u %t %r %s %b" />
  

  提示:修改完重启生效!!
  下午被***的日志:(筛选访问公司网站前10 的IP地址)
  

    [root@node2 ~]# awk -F "[ ]" '{print $1}' /home/wwwlogs/access.log |sort -nr|uniq -c|sort -nr|head -n 10  

DSC0002.jpg

  3.Nginx配置(安装不解释了)
  在Server标签下添加如下几行
  

set $allow true;  
if ($http_x_forwarded_for ~ "121.42.0.30|121.42.0.38|121.42.0.36|121.42.0.15121.42.0.37"){
  set $allow false;
  
}
  
if ($allow = false){
  return 404;
  
}
  

  #提示:IP添加在上面!
  小结: 因为无法禁止用户进行访问,我们设置404可以让IP无法进行访问数据库。不然数据库会被查询语句进行刷爆

三、Nginx开启目录浏览功能
  

[root@abcdocker extra]# cat nginx.conf  server {
  listen       80;
  server_name  IP地址;
  location / {
  root   html/bbs;                   #资源存放站点
  autoindex on;                         #开启目录浏览功能
  autoindex_localtime on;         #开启以服务器本地时区显示文件修改日期
  autoindex_exact_size off;       #关闭详细文件大小统计,让文件大小显示MB,GB单位,默认为b
  auth_basic "secret";
  auth_basic_user_file /usr/local/nginx/passwd.db;
  }
  }
  

  效果图
DSC0003.jpg

  如果有中文建议使用谷歌浏览器
  提示: 我们可以把某些资源放在这个目录下面,但是现在的结果是不安全! 下面我就为大家介绍nginx加密访问

四、Nginx用户通过用户名密码认证访问web站点

1.编辑虚拟主机配置文件
  

[root@rhel6u3-7 ~]# vim /usr/local/nginx/conf/vhost/ghzz.conf  
server {
  listen       80;  //监听端口为80
  server_name  www.qyt.com;  //虚拟主机网址
  location / {
  root   html/bbs;  //虚拟主机网站根目录
  index  index.html index.htm;  //虚拟主机首页
  auth_basic "secret";  //虚拟主机认证命名
  auth_basic_user_file /usr/local/nginx/passwd.db; //虚拟主机用户名密码认证数据库
  }
  location /status {
  stub_status on;  //开启网站监控状态
  access_log /usr/local/nginx/logs/www1_status.log; //监控日志
  auth_basic "NginxStatus"; }
  }
  

2.通过htpasswd命令生成用户名及对应密码数据库文件
  

[root@DB02 ~]# htpasswd -c /application/nginx/passwd.db qiuyt123  ##qiuyt123是用户名  
New password:   #这里输入的是密码
  
Re-type new password:
  
Adding password for user qiuyt123  #告诉你创建成功
  

  
[root@DB02 ~]# chown www. /application/nginx/passwd.db  #保证密码私密性授权最小,权限
  
[root@DB02 ~]# ll /application/nginx/passwd.db
  
-r-------- 1 www www 23 3月   2 11:12 /application/nginx/passwd.db
  

  
[root@DB02 ~]# cat /application/nginx/passwd.db  #查看用户密码记录
  
qiuyt123:gCVPy6CWIrQEs
  

3.DNS服务器上添加www A记录
  

w    A   1.1.1.1   ##使用的阿里云 ,A记录就是添加二级域名,然后映射到对应的IP地址  

DSC0004.jpg


4. 访问测试
  图是用的ck替换掉w进行访问测试
DSC0005.jpg


五、NGINX添加到环境变量

1.作用:
  环境变量就是把常用的服务添加到系统中去,可以在任何目录下去执行调用,让我们的操作更方便,不用刻意去记全局变量;

2.修改
  vi  /etc/profile文件,在文件末尾加上如下两行
  

PATH=$PATH:/usr/local/nginx/sbin  
export PATH
  
source /etc/profile
  

  最好使用source /etc/profile 使用 .. /etc/profile可能会不生效

六、NGINX日志切割脚本、及NGINX启动/停止脚本

1、Nginx 日志切割脚本
  

#!/bin/bash  
#URL=blog.51cto.com/blogger
  
#cut log file
  
LOG_FILE_LIST=/root/log_file_list.txt
  
for FILENAME in `cat $LOG_FILE_LIST`;do
  DATE=`date +%F`
  BACKUP_LOGFILE=${FILENAME}_${DATE}
  if [ -f $FILENAME ];then
  cat $FILENAME > $BACKUP_LOGFILE  &&  gzip $BACKUP_LOGFILE   && cat /dev/null > $FILENAME
  else
  echo "ERROR!$FILENAME is not exist."
  fi
  
done
  
find /data/logs/ -type f -name "*.log" -mtime +7 -exec rm -f {} \;  #保留最近7天的数据
  
find /data/logs/ -type f -name "*.gz" -mtime +7 -exec rm -f {} \;
  
#shell: cat /root/log_file_list.txt
  
/var/logs/nginx/access_www.log
  
#此处存放日志路径
  

2、Nginx启动停止脚本
  

#!/bin/bash  
# chkconfig: 2345 20 80           #这个选项设定此服务 开启及关闭的号
  
# description: Saves and restores system entropy pool for \
  
##############################################
  
#date:2017.8.8
  
#QQ:598759292
  
##http://blog.51cto.com/blogger#####################
  . /etc/init.d/functions
  
path=/usr/local/nginx/sbin/nginx  #如果编译自定义安全,需要修改软件安装路径
  
if [ $# -ne 1 ];then
  echo "please input {status|start|stop|restart|reload}"
  
fi
  
nginx_status(){
  
status=`lsof -i:80|wc -l`
  
if [ $status -gt 2 ];then
  echo "nginx is running "
  else
  echo  "nginx no running"
  
fi
  
}
  
##################
  
nginx_start(){
  $path
  if [ $? -eq 0 ];then
  action "nginx start" /bin/true
  else
  action "nginx no start" /bin/false
  fi
  
}
  
nginx_stop(){
  $path -s stop
  if [ $? -eq 0 ];then
  action "nginx stop" /bin/true
  else
  action "nginx no stop" /bin/false
  fi
  
}
  
nginx_restart(){
  $path -s stop
  if [ $? -eq 0 ];then
  action "nginx stop" /bin/true
  else
  action "nginx no stop" /bin/false
  fi
  sleep 3
  $path
  if [ $? -eq 0 ];then
  action "nginx start" /bin/true
  else
  action "nginx no start" /bin/false
  fi
  
}
  
nginx_reload(){

  $path -s>  if [ $? -eq 0 ];then

  action "nginx>  else

  action "nginx no>  fi
  
}
  
case "$1" in
  
start)
  nginx_start
  
;;
  
stop)
  nginx_stop
  
;;
  
restart)
  nginx_restart
  
;;
  
reload)
  nginx_reload
  
;;
  
status)
  nginx_status
  
;;
  
esac
  

七、nginx proxy_pass后的url加不加/的区别
  nginx配置proxy_pass,需要注意转发的路径配置
  第一种:proxy_pass后缀不加斜杠
  

location /abc/ {  proxy_pass http://172.16.1.38:8080;
  }
  

  第二种:proxy_pass后缀加斜杠
  

location /abc/ {  proxy_pass http://172.16.1.38:8081/;
  }
  

  两种配置区别:只在于proxy_pass转发的路径后是否带 /
  针对情况1,如果访问url = http://server/abc/test.jsp,则被nginx代理后,请求路径会变为http://proxy_pass/abc/test.jsp,将test/ 作为根路径,请求test/路径下的资源
  针对情况2,如果访问url = http://server/abc/test.jsp,则被nginx代理后,请求路径会变为 http://proxy_pass/test.jsp,直接访问server的根资源
  典型案例:
  

worker_processes  1;  
events {
  worker_connections  1024;
  
}
  
http {
  include       mime.types;
  default_type  application/octet-stream;
  sendfile        on;
  keepalive_timeout  65;
  
upstream app{
  server 172.16.1.38:8233;
  
}
  
upstream online{
  server 172.16.1.38:8239;
  
}
  
server {
  listen       881;
  server_name  IP;
  location /bxg/user/ {
  root   /root;
  index  index.html index.htm;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  client_max_body_size 100m;
  proxy_pass  http://online;
  
解释:当我们访问http://IP/881/bxg/user/下面的资源,nginx会帮我们跳转到online下面对应的IP+端口
  
此时返回的url =http://IP/881/bxg/user/1.txt
  }
  location /bxg/app/ {
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  client_max_body_size 100m;
  proxy_pass  http://app/;
  
解释:当我们访问http://IP/881/bxg/app/下面的资源(此时proxy_pass后面带斜杠),nginx也会帮我们跳转到app下面对应的IP+端口
  
此时返回的url =http://IP/881/1.txt
  }
  
#这行属于默认匹配,就是后面什么也不添加,881端口就直接调用这个项目
  location / {
  root   /root;
  index  index.html index.htm;
  proxy_set_header Host $http_host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  client_max_body_size 100m;
  proxy_pass  http://app;
  }
  
}
  

  提示:这种location常用于只有一个公网IP和端口场景,内网IP没有进行映射,但是又需要请求我们的内网服务器的服务,就可以使用location的模式。
  location常用的场景还有类似于专题页面
  

        location /a{  root  /data/online-active-page/web/subject;
  index  itcast.html;
  }
  location /b{
  root  /data/online-active-page/web/subject;
  index  itheima.html;
  }
  

  当访问https://域名/a/ 匹配对应/data/online-active-page/web/subject下文件
  当访问https://域名/b/ 匹配对应/data/online-active-page/web/subject下文件

八、Nginx常用rewrite

1、页面跳转
  当我们想输入https://qiuyuetao.com/web/1/1.html 直接页面跳转到https://qiuyuetao.com/class
  可以采用下面方式
  

rewrite ^/web/1/1.html https://abcdocker.com/class permanent;  
rewrite ^/web/microCla***oom/microCla***oom.html https://abcdocker.com/course/micro permanent;
  
rewrite ^/web/freeofcharge/freeofcharge.html https://abcdocker.com/course/free permanent;
  
rewrite ^/web/html/ansAndQus.html https://abcdocker.com/ask permanent;
  

  提示:场景常用语开发代码调用接口

2、整站https
  常用的有以下rewrite
  

rewrite ^(.*) https://$host$1 permanent;  
return      301 https://$server_name$request_uri;



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-632433-1-1.html 上篇帖子: Nginx支持CGI-milanlinux 下篇帖子: Nginx的介绍和部署
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表