nginx笔记

1、安装

yum安装

官网教程:http://nginx.org/en/linux_packages.html#RHEL-CentOS

安装yum工具包

sudo yum install -y yum-utils

创建repo文件

vim /etc/yum.repos.d/nginx.repo

把下面的内容复制进去

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

保存退出后,开始安装

sudo yum install -y nginx

安装位置

#程序位置
/usr/sbin/nginx

#配置文件位置
/etc/nginx 

2、命令

1、查看进程id

ps -ef | grep nginx	

2、信号

立刻关闭服务(发送给master进程)

kill -TERM/INT pid

优雅的关闭服务

kill -QUIT pid

重新读取配置文件

kill -HUP pid

重新打开日志文件

kill -USR1  `命令`

所有的子进程不再接收新的连接,相当于给work进程发送QUIT命令

kill -WINCH  pid

3、命令行

查看版本

nginx -V

nginx -v

检测nginx配置文件有没有出错

nginx -t

(常用)用命令发送信号

nginx -s quit             #worker进程优雅的退出

nginx -s reload           #重新加载配置文件

nginx -s stop             #直接退出

指定配置文件位置

nginx -c  filename

3、配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


    http {
        include       /etc/nginx/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  /var/log/nginx/access.log  main;

        sendfile        on;
        #tcp_nopush     on;

        keepalive_timeout  65;

        #gzip  on;

        server {
        listen       80;
        server_name  localhost;

        #access_log  /var/log/nginx/host.access.log  main;

        location / {
            root   /usr/share/nginx/html;
            index  index.html index.htm;
        }

        #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   /usr/share/nginx/html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }

}

1、全局

1、user

如果worker进程所属用户不是root,可能会在访问某些资源的时候报权限不足的错误。

配置文件的首行

user  nginx;

标识worker进程所属用户是nginx

2、work process

master_process用来指定是否开启工作进程,默认值是on,如果要关闭:(一般不修改)

master_process off;

如果修改此配置,需要关闭nginx再启动,不要用reload

nginx -s stop
nginx

worker_processes,配置worker进程的数量,如果是auto表示和系统的核心数一致

worker_processes  auto;

3、daemon off

默认情况下是 daemon on

表示守护进程,后台运行,不占用客户端

4、include

可以将重复的配置放到一个文件里,然后引进来

2、events

1、accpet_mutex

默认值 on

比如说有一个请求过来,Nginx可能会唤醒多个worker,某些场景下会浪费资源。

设置此变量为on,则可以一个一个唤醒worker进程,不会一下子全部唤醒。

accpet_mutex on

2、multi_accept

默认值是off

如果被禁止了,则表示一个worker进程同时只能接受一个新的连接。如果打开状态,一个工作进程可以接受所有的新连接。

生产环境建议打开

3、worker connections

配置单个worker进程最大的连接数,例如

worker_connections	1024;

此数值不能大于操作系统支持打开的最大文件句柄数量。

3、use

根据自己的操作系统来

可选值

select/poll/epoll/kqueue

3、http

1、MIME-Type

include       /etc/nginx/mime.types;
default_type  application/octet-stream;

mime.types这个文件里面放的是一些媒体资源的类型,比如jpg,png,gif?

default_type可以放在http、server、location等位置

比如我想要向浏览器输出hello, world
修改下配置文件

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/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  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    #include /etc/nginx/conf.d/*.conf;

    server{
        listen 80;
        server_name localhost;
        location / {
                default_type text/html;
                return 200 "<h1>Hello, World!</h1>";
        }
    }
}

重启nginx,浏览器访问地址,可以看到:
浏览器页面

2、log_format

定义日志的格式

3、access_log

配置日志的目录,以及日志的格式

access_log  /var/log/nginx/access.log  main;

可以在多个地方设置access_log,根据就近原则生效

4、sendfile

对应linux的系统函数,默认值是off

如果开启,可以使用sendfile()传输文件,提高nginx处理静态资源的速度。建议开启

sendfile on

5、keepalive_timeout

用来设置长连接的超时时间

默认

keepalive_timeout 75s
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值