centos7.9安装openrestry

简介:openrestry是一个与lua脚本结合的高性能服务器

一:根据官网下载安装包

https://openresty.org/cn/linux-packages.html

centos7.9的安装命令

# add the yum repo:
wget https://openresty.org/package/centos/openresty.repo
sudo mv openresty.repo /etc/yum.repos.d/openresty.repo

# update the yum index:
sudo yum check-update

sudo yum install -y openresty

sudo yum install -y openresty-resty

sudo yum --disablerepo="*" --enablerepo="openresty" list available

 

 

二..配置环境

sudo nano /etc/profile

在文件最后一行添加

export PATH=/usr/local/openresty/nginx/sbin:$PATH

退出环境变量生效

source /etc/profile

默认安装后的80端口会被开放,查看并杀死

netstat -lnpt | grep 80

kill -9 pid

 

三.简单例子:

https://openresty.org/cn/getting-started.html

具体操作

1,进入工作目录,如果没有,创建一个新的

cd /root/nginx

2.创建对应的工作目录

mkdir logs/ conf/

3.编写配置文件

nano conf/nginx.conf

worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 8080;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("<p>hello, world</p>")
}
}
}
}

 

4.相关命令

启动命令 -p指定目录,现在是pwd当前目录
nginx -p `pwd`/ -c conf/nginx.conf
重新加载配置文件
nginx -p `pwd`/ -s reload

停止命令

nginx -p `pwd`/ -s stop

5.测试

curl http://localhost:8080/

 四.真正启动nignx

1.查看nginx信息,查看默认html等

nginx -V
查找html,看--prefix,然后找到对应的 --prefix/html/index.html,查看默认工作目录等

2.进入默认的工作目录

cd /usr/local/openresty/nginx/

 

3修改默认的配置文件

nano conf/nginx.conf

简单示例

#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  on;

    server {
        listen       80;
        server_name  localhost;
#客户端的最大上传文件大小
        client_max_body_size 20M;
#客户端的最大上传文件大小,不产生临时文件
        client_body_buffer_size 20M;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

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

        #error_page  404              /404.html;
        location /api {
            rewrite ^.+api/?(.*)$ /$1 break; #去掉后端接口没有的/api
           # add_header 'Access-Control-Allow-Origin' '*';
           # add_header 'Access-Control-Allow-Headers' '*';
           # add_header 'Access-Control-Allow-Methods' '*';
            # OPTIONS 直接返回204
            #if ($request_method = 'OPTIONS') {
            # return 204;}
            proxy_pass http://127.0.0.1:8080; #反向代理到后端
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            #这个Header和X-Real-IP类似,但它在多级代理时会包含真实客户端及中间每个代理服务器的IP
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #表示客户端真实的协议(http还是https)
            proxy_set_header X-Forwarded-Proto $scheme;
        }
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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;
        #}
    }


    # 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;
    #    }
    #}

}
View Code

 

4.启动nginx

nginx -c conf/nginx.conf

5.停止nginx

nginx -s stop

6.设置开机启动

systemctl enable openresty.service

7.设置浏览器无缓存

add_header Cache-Control "no-cache, no-store, must-revalidate";
add_header Pragma "no-cache";
add_header Expires "0";

8.nignx相关命令

检查配置文件

sudo nginx -t 

重新加载配置文件

sudo nginx -s reload

9,日志切分脚本

创建脚本

touch /usr/local/openresty/nginx/logs/split.sh

#!/bin/bash
nginx_access_log="/usr/local/openresty/nginx/logs/access.log" # Nginx访问日志的位置
log_backup_dir="/usr/local/openresty/nginx/logs/bak" # 日志备份存放的目录
current_date=$(date +%Y%m%d) # 获取当前日期

# 确保日志备份目录存在
if [ ! -d "$log_backup_dir" ]; then
    mkdir -p "$log_backup_dir"
fi

# 切割日志并备份
mv $nginx_access_log ${log_backup_dir}/$current_date.log
nginx -s reopen # 重新打开日志文件

# 删除30天以前的日志文件
find $log_backup_dir -name "*.log" -type f -mtime +30 -exec rm -f {} \;

 10开启定时任务

crontab -e

每天下午5点执行日志切分脚本

PATH=/usr/local/openresty/nginx/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java8/jre/bin:/root/bin
0 17 * * * /usr/local/openresty/nginx/logs/split.sh

crontab -l

查看定时任务

posted @ 2024-05-28 10:16  24601  阅读(79)  评论(0编辑  收藏  举报