Nginx 相关内容 [持续补充2017.09.06更]
Nginx 相关内容
Nginx 相关内容 [持续补充]
Nginx
version: 2017.09-01-更
一、启动Nginx目录浏览功能
[root@abcdocker extra]# cat w.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 相关内容 [持续补充2017.09.06更]-每日运维网 Nginx 相关内容 [持续补充2017.09.06更]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504043643134.png)
如果有中文建议使用谷歌浏览器
提示: 我们可以把某些资源放在这个目录下面,但是现在的结果是不安全! 下面我就为大家介绍nginx加密访问
二、Nginx实战之让用户通过用户名密码认证访问web站点
1.编辑虚拟主机配置文件
[root@rhel6u3-7 ~]# vim /usr/local/nginx/conf/extra/w.conf
server {
listen 80; //监听端口为80
server_name w.abcdocker.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@abcdocker extra]# htpasswd -c /usr/local/nginx/passwd.db abcdocker New password: Re-type new password: Adding password for user abcdocker [root@abcdocker extra]# chmod 400 /usr/local/nginx/passwd.db [root@abcdocker extra]# chown nginx. /usr/local/nginx/passwd.db [root@abcdocker extra]# cat /usr/local/nginx/passwd.db abcdocker:$apr1$w3RqjNXW$mSiazU7t2THQqo3LrPmlP/
3.DNS服务器上添加www A记录
w A 1.1.1.1
![Nginx 相关内容 [持续补充2017.09.06更]-每日运维网 Nginx 相关内容 [持续补充2017.09.06更]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504123646455.png)
4.访问测试

三、NGINX添加到环境变量
正常安装后执行nginx需要 ./nginx
这太麻烦了,所以要把它加到环境变量里去,这样随便在哪个目录都可以直接执行nignx 也不用进到nginx/sbin目录
修改/etc/profile文件,在文件末尾加上如下两行
PATH=$PATH:/usr/local/nginx/sbin export PATH
最好使用source /etc/profile 使用
.. /etc/profile可能会不生效
四、NGINX日志切割脚本、及NGINX启动/停止脚本
1、Nginx 日志切割脚本
#!/bin/bash
#URL=www.abcdocker.com
#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 {} ;
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:2016.8.8
#QQ:604419314
#blog:www.abcdocker.com######################
. /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
五、SLB场景使用Nginx封用户真实IP
场景: 前端SLB--->nginx---->proxy
![Nginx 相关内容 [持续补充2017.09.06更]-每日运维网 Nginx 相关内容 [持续补充2017.09.06更]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504123647520.png)
1.首先需要配置SLB(阿里云负载均衡)让slb记录用户真实IP功能
![Nginx 相关内容 [持续补充2017.09.06更]-每日运维网 Nginx 相关内容 [持续补充2017.09.06更]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504123648503.png)
此处勾选即可
创建SLB-官方文档
2.tomcat开启X-Forwarded-For日志功能
开启tomcat的X-Forwarded-For,在tomcat/conf/server.xml中,修改AccessLogValve日志纪录功能为如下内容:
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" />
提示:修改完重启生效!!
下午被攻击的日志:
![Nginx 相关内容 [持续补充2017.09.06更]-每日运维网 Nginx 相关内容 [持续补充2017.09.06更]-每日运维网](https://img.mryunwei.com/uploads/2023/05/20230504123649693.png)
3.Nginx配置(安装不解释了)
在Server标签下添加如下几行
set $allow true;
if ($http_x_forwarded_for ~ "106.121.*.*|106.121.71.120|106.121.77.28|106.121.74.130|218.109.235.254"){
set $allow false;
}
if ($allow = false){
return 404;
}
#提示:IP添加在上面!
小结: 因为无法禁止用户进行访问,我们设置404可以让IP无法进行访问数据库。不然数据库会被查询语句进行刷爆
六、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://abcdocker.com/a/ 匹配对应/data/online-active-page/web/subject下文件
当访问https://abcdocker.com/b/ 匹配对应/data/online-active-page/web/subject下文件
七、常用rewrite
当我们想输入https://abcdocker.com/web/1/1.html 直接页面跳转到https://abcdocker.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;
提示:场景常用语开发代码调用接口
整站htps
常用的有以下rewrite
rewrite ^(.*) https://$host$1 permanent; return 301 https://$server_name$request_uri;
相关文章:
- 老男孩Shell企业面试题30道 [答案]
- Kubernetes 1.14 二进制集群安装
- [NGINX] – 配置文件优化 – NGINX.CONF
- Kuerbernetes 1.11 集群二进制安装