Nginx配置介绍(简单使用)

配置结构

  • main 全局配置
    • event 配置工作模式以及连接数
    • http http模块相关配置
      • server 虚拟主机配置,可以有多个

核心配置文件

常见问题

常用命令

日志切割

访问静态资源

Nginx 跨域配置支持

防盗链配置支持




#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

    #    ssl_session_cache    shared:SSL:1m;
    #    ssl_session_timeout  5m;

    #    ssl_ciphers  HIGH:!aNULL:!MD5;
    #    ssl_prefer_server_ciphers  on;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
1.配置worker进程的用户,指的linux中的用户,会涉及到nginx操作目录或文件的一些权限,默认为nobody
user root
2.worker进程工作数量设置,一般来说CPU有几个就设置几个,或者设置为N-1
worker_processes 1;
3.nginx日志级别`debug | info | notice | warn | error | crit | alert | emerg 错误级别从左到右越来越大
4.设置nginx进程pid
pid    logs/nginx.pid;
5.设置工作模式
event{
    #默认使用epoll
    use epoll;
    #每个worker允许连接的客户端最大连接数
    worker_connections 10240
}
6.http是指令块,针对http网络传输的一些指令配置
http{
}
7.include引入外部配置,提高可读性,避免单个配置文件过大
include    mine.types;
8.设置日志格式,main为定义的格式名称,如此access_log就可以直接使用这个变量了:
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
参数名 参数意义
$remote_addr 客户端ip
$remote_user 远程客户端用户名,一般为:'-'
$time_local 时间和时区
$request 请求的url以及method
$status 响应状态码
$body_bytes_send 响应客户端内容字节数
$http_referer 记录用户从哪个连接跳转过来的
$http_user_agent 用户使用的代理,一般来时都是浏览器
$http_x_forwarded_for 通过代理服务器来记录客户端的ip
9.sendfile使用高效文件传输,提升传输性能,启用后才能使用tcp_nopush,是指当数据表累计一定大小后才发送,提高效率:
sendfile      on;
tcp_nopush    on;
10.keepalive_timeout设置客户端与服务端请求的超时时间,抱着客户端多次请求的时候不会重复建立新的链接,节约资源损耗:
keepalive_timeout    65;
11.gzip启用压缩,html/css压缩后传输会更快
gzip    on;
12.server可以http指令块中设置多个虚拟主机
  • listen 监听端口
  • server_name localhost,ip,域名
  • location 请求路由映射,匹配拦截
  • root 请求位置
  • index 首页设置
server{
    listen    80;
    server_name    localhost;
    location  / {
        root html;
        index  index.html   index.htm;
    }
}


常见问题

  • linux端口解决
1.查看开放的端口号
firewall-cmd --list-all
2.设置开放端口号
sodu firewall-cmd --add-port=80/tcp --permanent
3.重启防火墙
firewall-cmd -reload
  • nginx.pid打开失败, 提示没有此文件或文件夹
cd到提示文件路径,此时提示没有此文件或文件夹,直接创建文件夹(mkdir 文件路径)

  • invaild PID Number...
手动设置配置文件
nginx -c 文件名


常用命令

  • 查看nginx是否启动
ps -ef | grep nginx
  • 停止nginx
nginx -s stop
  • 重启
nginx -s reload
  • 暴力退出
 nginx -s stop
  • http请求完毕后退出
nginx -s quit
  • 检查配置文件是否失效
nginx -t
  • 查看版本号
nginx -v
  • 查看nginx具体信息
nginx -V 
  • 帮助
nginx -?
nginx -h
  • 手动设置配置文件
nginx -c 文件名
[root@localhost local]# ./nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
configure arguments: 
--prefix=/usr/local/nginx 
--pid-path=/var/run/nginx/nginx.pid 
--lock-path=/var/lock/nginx.lock 
--error-log-path=/var/log/nginx/error.log 
--http-log-path=/var/log/nginx/access.log 
...


日志切割

通过查看配置可以看到日志文件存放在access.log中,随着时间的推移这个文件会越来越大,不便于运维人员查看,所以我们可以将此文件根据时间的规则切割成不同的小文件

手动切割
  • 创建一个shell可执行文件:cut_my_log.sh内容为:
#!/bin/bash
LOG_PATH="/var/log/nginx/"
RECORD_TIME=$(date -d"yesterday" +%Y-%m-%d+%H:%M)
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`
  • 为该文件添加权限:
chmod +x cut_my_log.sh
  • 运行该文件
./cut_my_log.sh
定时切割
  • 安装定时任务(crontabs):
yum install crontabs
  • crontab -e 编辑并且添加一行新的定时任务(crontab -l查看定时任务列表):
*/1 * * * *  /usr/local/nginx/sbin/cut_my_log.sh
  • 重启定时任务
service crond restart

常用定时任务命令:

service crond start                  //启动任务
service crond stop                   //关闭任务
service crond resstart               //重启任务
service crond reload                 //重新载入配置
crond -e                             //编辑任务
crond -l                             //查看任务列表

常见表达式:

  • 每分钟执行:
*/1 * * * * 
  • 每日凌晨(每天晚上23:59)执行:
59 23 * * * 
  • 每天凌晨1点执行:
0 1 * * * 


访问静态资源

root与alias

假如服务器路径为:/home/source/img/face.png

  • root 路径完全匹配访问
    配置的时候为:
location /source{
    root  /home
}

为:url:port/source/img/face.png

  • alias可以为路径做一个别名
    配置的时候为:
location /picture{
    alias  /home/source/img
}

用户方位时候的请求为:url:port/pictrue/face.png

image.png

这里先将要访问的资源放到了/home/source目录下,然后去配置nginx.conf文件的server

    server{
        listen 90;
        server_name localhost;

        location /{
                root /home/foodie-shop;
                index   index.html;
                }
        location /source{
                 root /home;
                }
        location /static{
                alias   /home/source;
                }
        }
  • 首先配置端口号为90,servername为localhost
  • 第一个location配置的是一个前端项目 ,只有/ ,那就可以通过localhost:90访问到/home/foodie-shop下的index.html文件了
  • 第二个配置/source会被拼接到/home后,这样就可以通过localhost:90/source访问到/home/souce下的文件
  • 第三个通过配置别名alias,使用static访问到/home/souce的文件
    image.png

Nginx 跨域配置支持:

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

防盗链配置支持

对源站点验证

valid_referers*.baidu.com
\
#非法引入会进行下方判断
if($invalid_referer){
  return 404;
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,524评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,869评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,813评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,210评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,085评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,117评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,533评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,219评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,487评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,582评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,362评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,218评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,589评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,899评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,176评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,503评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,707评论 2 335

推荐阅读更多精彩内容