您的位置:首页 > 运维架构 > Nginx

nginx 配置优化指南-相关内容【四】

2017-12-26 16:30 561 查看

一、Nginx后台项目限制外网访问

公司合规要求,后台地址限制外网访问,所以通过匹配进行限制,但通过nginx deny allow进行设置无法生效,故通过用户真实IP进行判断,步骤如下:
1)环境介绍:
场景: 前端SLB--->nginx---->proxy



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



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日志纪录功能为如下内容:

<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
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




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;
}
}

效果图



如果有中文建议使用谷歌浏览器

提示: 我们可以把某些资源放在这个目录下面,但是现在的结果是不安全! 下面我就为大家介绍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地址




4. 访问测试

图是用的ck替换掉w进行访问测试



五、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 reload
if [ $? -eq 0 ];then
action "nginx reload" /bin/true
else
action "nginx no reload" /bin/false
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/microClassroom/microClassroom.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;[/code] 
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  web 服务 nginx