centos7.9安装openrestry
简介:openrestry是一个与lua脚本结合的高性能服务器
一:根据官网下载安装包
https://openresty.org/cn/linux-packages.html
二..配置环境
export PATH=/usr/local/openresty/nginx/sbin:$PATH
默认安装后的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.进入默认的工作目录
cd /usr/local/openresty/nginx/
2.查看nginx信息,查看默认html等
nginx -V
查找html,看--prefix,然后找到对应的 --prefix/html/index.html
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; #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; # } #} }
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";