nginx 基础知识

2021-03-21  本文已影响0人  AD刘涛

什么是反向代理

用户请求目标服务器,由代理服务器决定使用哪个服务器进行服务。

反向代理

图解正向代理、反向代理

apache 劣势

每个请求会独占一个工作线程,当并发数到几千时,就同时有几千的线程在处理请求了。这对操作系统来说,线程带来的内存占用非常大,线程的上下文切换带来的cpu开销很大,自然性能就上不去了,而这些开销完全没有意义。

nginx 常见配置

server {
        # 只有本机5000端口才能访问该服务
        listen       127.0.0.1:5000;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 普通匹配 只有输入127.0.0.1:5000才能访问
        # location / {
        #     root   /www/foodie-shop;
        #     index  index.html index.htm;
        # }

        # # 精确匹配 只有输入127.0.0.1:5000/WechatIMG53.jpeg 才能访问
        # location = /WechatIMG53.jpeg {
        #     root   /www/image;
        #     index  index.html index.htm;
        # }

        # 匹配正则表达式 * 代表不区分大小写 
        location ~* .(GIF|jpg|jpeg|png) {
            root   /www/image;
        }

Nginx 日志切割

  1. 创建一个shell可执行文件:cut_my_log.sh
#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d "yesterday" +%Y-%m-%d)
PID=/var/run/nginx/nginx.pid
mv ${LOG_PATH}/access.log ${LOG_PATH}/access.${RECORD_TIME}.log
mv ${LOG_PATH}/error.log ${LOG_PATH}/error.${RECORD_TIME}.log

#向Nginx主进程发送信号,用于重新打开日志文件
kill -USR1 `cat $PID`

  1. cut_my_log.sh 添加可执行权限
chmod +x cut_my_log.sh
  1. 执行脚本
./cut_my-log.sh

Nginx 跨域配置

#允许跨域请求的域,*代表所有
add_header 'Access-Control-Allow-Origin' *;
#允许带上cookie请求
add_header 'Access-Control-Allow-Credentials' 'true';
#允许请求的方法,比如 GET/POST/PUT/DELETE
add_header 'Access-Control-Allow-Methods' *;
#允许请求的header
add_header 'Access-Control-Allow-Headers' *;

Nginx 防盗链接配置

#对源站点验证
valid_referers *.imooc.com; 
#非法引入会进入下方判断
if ($invalid_referer) {
    return 404;
} 

如:

 server {
        # 只有本机5000端口才能访问该服务
        listen       127.0.0.1:5000;
        server_name  localhost;


        #允许跨域请求的域,*代表所有
        add_header 'Access-Control-Allow-Origin' *;
        #允许带上cookie请求
        add_header 'Access-Control-Allow-Credentials' 'true';
        #允许请求的方法,比如 GET/POST/PUT/DELETE
        add_header 'Access-Control-Allow-Methods' *;
        #允许请求的header
        add_header 'Access-Control-Allow-Headers' *;

        #对源站点验证
        valid_referers *.imooc.com; 
        #非法引入会进入下方判断
        if ($invalid_referer) {
            return 404;
        } 

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        # 普通匹配
        location / {
            root   /Users/liutao/Github/foodie-shop;
            index  index.html index.htm;
        }

        # 精确匹配
        location = /WechatIMG53.jpeg {
            root   /Users/liutao/Documents/照片;
            index  index.html index.htm;
        }

        # 匹配正则表达式 * 代表不区分大小写
        location ~* .(GIF|jpg|jpeg|png) {
            root   /Users/liutao/Documents/照片;
        }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}
上一篇 下一篇

猜你喜欢

热点阅读