公司合规要求,后台地址限制外网访问,所以通过匹配进行限制,但通过nginx deny allow进行设置无法生效,故通过用户真实IP进行判断,步骤如下:
一、Nginx开启目录浏览功能
[root@node01 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=https://www.dgstack.cn/archives/989.html
#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
##https://www.dgstack.cn###############
. /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 /qyt/ {
proxy_pass http://172.16.1.38:8080;
}
第二种:proxy_pass后缀加斜杠
location /qyt/ {
proxy_pass http://172.16.1.38:8081/;
}
两种配置区别:只在于proxy_pass转发的路径后是否带 /
针对情况1,如果访问url = http://server/qyt/test.jsp,则被nginx代理后,请求路径会变为http://proxy_pass/qyt/test.jsp,将test/ 作为根路径,请求test/路径下的资源
针对情况2,如果访问url = http://server/qyt/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://dgstack.com/class permanent;
rewrite ^/web/microCla***oom/microCla***oom.html https://dgstack.com/course/micro permanent;
rewrite ^/web/freeofcharge/freeofcharge.html https://dgstack.com/course/free permanent;
rewrite ^/web/html/ansAndQus.html https://dgstack.com/ask permanent;
提示:场景常用语开发代码调用接口
2、主域名跳转到二级域名(类似泛解析)
server {
server_name dgstack.com;
rewrite ^(.*) https://www.dgstack.com$1 permanent;
}
3、整站https
常用的有以下rewrite
rewrite ^(.*) https://$host$1 permanent;
return 301 https://$server_name$request_uri;
七 nginx upstream response is buffered 问题
nginx+tomcat 反向代理的架构
通过日志发现,在nginx与tomcat交互时,由于请求量有波动,在比较大的请求量,就会触发buffered,但是之前没有设置proxy相关的参数,只是设置了超时时间,所以采用默认的4K 内存页,进行缓存,故出现此问题,下面针对proxy相关参数说明
#在http标签下,nginx.conf,也可以在子配置文件设置
proxy_connect_timeout 3; #通过代理链接的超时时间,最大不能超时75s。
proxy_read_timeout 60; #从代理服务器读取的响应超时时间,如果单位时间内没有请求,则关闭链接。
proxy_send_timeout 60; #将请求发送到代理的超时时间,用于判断2个连续写操作的间隔。
proxy_buffer_size 64k; #读取代理响应头第一部分的大小,默认是4K 到 8k,解决数据量过大,比如header过大的问题
proxy_buffering on #默认是开启的,只有开启下面的2个才生效
proxy_buffers 4 32k; #读取代理请求的个数和缓冲区大小。
proxy_busy_buffers_size 64k; #繁忙时 buffer 的最大值
proxy_temp_file_write_size 128k; #临时文件写入的大小
#主要是看清报错日志,是proxy,还是fastcgi或 uwsgi,自定义优化参数
关于这几个配置的官方文档链接如下
- QQ精品交流群
-
- 微信公众号
-