51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

Nginx学习记录

Nginx

1、nginx安装

> Nginx下载

wget https://nginx.org/download/nginx-1.24.0.tar.gz

> 解压nginx压缩包

tar -zxvf nginx-1.24.0.tar.gz

> 介绍各目录

auto  为了辅助conf文件执行,表示nginx有什么功能
CHANGES 表示ngixn每个版本有什么特性
CHANGES.ru  俄罗斯语言
conf  示例文件
configure 用来生成中间文件,安装的必备动作./configure
contrib 
html  提供了标志的html文件
LICENSE
man   nginx帮助文件
README
src  nginx的源代码

> 编译

# 查看configure支持哪些参数
./configure --help|more
# 指定路径安装
./configure --prefix=/jorry/domains/nginx
# 执行编译
make
make install
# 如果出现缺少gcc环境信息
yum -y install gcc openssl openssl-devel pcre-devel zlib zlib-devel

2、nginx配置语法

1、配置文件由指令与指令块构成
2、每条指令以;分号结尾,指令与参数间以空格符号分隔指令块以}大括号将多条指令组织在一起
3、include语句允许组合多个配置文件以提升可维护性使用#符号添加注释,提高可读性
4、使用S符号使用变量
5、部分指令的参数支持正则表达式
http {
    include mime.types;
    upstream thwp {
        server 127.0.0.1:8000;
    }
    server {
        listen 443 http2;
        #Nginx配置语法
        limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s;
        location~*\.(gifljpgljpeg)$proxy_cache my_cache;expires 3m; 
        proxy_cache_key $host$uri$is _args$args ;
        proxy_cache_valid 200 304 302 1d;
         proxy_pass http: / /thwp
}

3、启动nginx

> nginx命令行

1、格式
	nginx -s reload
2、帮助
   -? -h
3、使用指定的配置文件
  -c
4、指定配置命令
  -g
5、指定运行目录
  -p
6、发送信号
  -s  (立刻停止 stop;优雅的停止服务 quit;重载配置文件 reload;重新开始记录日志文件:reopen)
7、测试配置文件是否有语法错误:
  -t -T
8、打印nginx的版本信息,编译信息
  -v -V

> 热部署

# 1、首先复制出一个/sbin/nginx
root       7750      1  0 11:15 ?        00:00:00 nginx: master process ./nginx
nobody     7751   7750  0 11:15 ?        00:00:00 nginx: worker process
root       7780   3610  0 11:17 pts/2    00:00:00 grep --color=auto ngin
# 2、发送指令 kill -USR2 xxx原进程id
root       7750      1  0 11:15 ?        00:00:00 nginx: master process ./nginx
nobody     7751   7750  0 11:15 ?        00:00:00 nginx: worker process
root       7813   7750  0 11:18 ?        00:00:00 nginx: master process ./nginx
nobody     7814   7813  0 11:18 ?        00:00:00 nginx: worker process
root       7816   3610  0 11:19 pts/2    00:00:00 grep --color=auto nginx
# 发送kill -WINCH xxx原进程id 告诉老的nginx进程,请优雅关闭所有的work进程

4、部署项目

#user  nobody;
worker_processes  4;

#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压缩
    gzip  on;

    server {
        listen       8989; #设置监听端口
        server_name  localhost; 
        #限流
        set $limit_rate 1k;
        #charset koi8-r;
        #设置日志记录的路径以及文件
        access_log  logs/host.access.log  main;

        location / {
           alias    /project/dist/; # 部署项目路径
           # root   html;
           # index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

> 代理服务器

  upstream local{
      server 127.0.0.1:8989;
    }
 server {
        listen      80
        server_name  www.hezhenbin.cn;
        #限流
        set $limit_rate 1k;
        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
           proxy_set_header Host $host;
           proxy_set_header X-Real-IP $remote_addr;
           proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
           #设置缓存
           proxy_cach my_cache;
           proxy_cache_key $host$uri$is_args$args;
           proxy_cache_valid 200 304 302 1d;
           proxy_pass http://local;
        }
}
赞(5)
未经允许不得转载:工具盒子 » Nginx学习记录