【后端】Nginx+lua+OpenResty高性能实践

文章目录


【后端&网络&大数据&数据库目录贴】

1.介绍

1.1 常用版本

常用版本分为四大阵营

在这里插入图片描述

1.2 其他web服务器

IIS、Apache Httpd1/2、lighttpd

1.3 nginx安装

1.3.1 离线安装

  • 普通(需联网)

将nginx包解压到linux中
./configure --prefix=/usr/local/nginx
报错:安装pcre: yum install -y pcre pcre-devel
报错安装zlib:yum install -y zlib zlib-devel
make & make install
**重新构建要 make clean清空缓存**

  • 普通(无需联网)

https://blog.csdn.net/achi010/article/details/106392040
./configure --prefix=/usr/local/nginx --with-http_ssl_module
--with-pcre=../pcre-8.37
--with-zlib=../zlib-1.2.11
--with-openssl=../openssl-1.1.0e
make && make install
其他说明:
(prefix用绝对路径,不然后面访问出错)
--without-http_rewrite_module忽略pcre包
--without-http_gzip_module忽略zlib包
如果配置https的网站,需要配置ssl命令--with-http_ssl_module
ssl:--with-http_ssl_module 一般和--with-openssl=xxx共用,否则会使用系统的openssl
nginx -V 可以查看各个模块
如果忘记配置ssl,则可以用另一种方式
在这里插入图片描述

  • 普通(无需联网)

2. 命令

启动 ./nginx
快速停止:./nginx -s stop
优雅关闭,在退出前完成已经接受的连接请求:./nginx -s quit
重新加载配置:./nginx -s reload
检查配置文件正确性: ./nginx -t
总结常用命令
nginx -v 查看版本号
./nginx //启动命令
./nginx -s stop //关闭命令
./nginx -s reload //重启命令
查看模块配置:./nginx -V
指定配置文件:./nginx -c conf/nginx-88.conf
指定配置文件检测:./nginx -c /app/openresty/11581/nginx/conf/nginx_http_9060_101.conf -t
/user/opt/openresty/nginx/sbin/nginx -c /user/opt/openresty/nginx/conf/nginx_http_9061.conf -t
指定配置文件重启./nginx -c /app/openresty/11581/nginx/conf/nginx_http_9060_101.conf -s reload
/user/opt/openresty/nginx/sbin/nginx -c /user/opt/openresty/nginx/conf/nginx_http_9061.conf -s reload

4. DNS解析过程

在这里插入图片描述

3. 配置

3.1 详解

配置详解
########### 每个指令必须有分号结束。#################
`#user administrator administrators;` #配置用户或者组,默认为nobody nobody。
#worker_processes 2; #允许生成的进程数,默认为1
`#pid /nginx/pid/nginx.pid`; #指定nginx进程运行文件存放地址
`error_log log/error.log debug`; #制定日志路径,级别。这个设置可以放入全局块,`http`块,`server`块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg
events {
accept_mutex on; #设置网路连接序列化,防止惊群现象发生,默认为on
multi_accept on; #设置一个进程是否同时接受多个网络连接,默认为off
#use epoll; #事件驱动模型,
select|poll|kqueue|epoll|resig|/dev/poll|eventport
worker_connections 1024; #最大连接数,默认为512
}
http {
	include mime.types; #文件扩展名与文件类型映射表
	default_type application/octet-stream; #默认文件类型,默认为	   text/plain
#access_log off; #取消服务日志
log_format myFormat '$remote_addr–$remote_user [$time_local] $request
$status $body_bytes_sent $http_referer $http_user_agent $http_x_forwarded_for';
#自定义格式
access_log log/access.log myFormat; #combined为日志格式的默认值
sendfile on; #允许sendfile方式传输文件,默认为off,可以在http块,server块,
location块。
sendfile_max_chunk 100k; #每个进程每次调用传输数量不能大于设定的值,默认为0,即不设上
限。
keepalive_timeout 65; #连接超时时间,默认为75s,可以在http,server,location块。
upstream mysvr {
server 127.0.0.1:7878;
server 192.168.10.121:3333 backup; #热备
}
error_page 404 https://www.baidu.com; #错误页
server {
keepalive_requests 120; #单连接请求上限次数。
listen 4545; #监听端口
server_name 127.0.0.1; #监听地址
location ~*^.+$ { #请求的url过滤,正则匹配,~为区分大小写,~*为不区分大小
写。
#root path; #根目录
#index vv.txt; #设置默认页
proxy_pass http://mysvr; #请求转向mysvr 定义的服务器列表
deny 127.0.0.1; #拒绝的ip
allow 172.18.5.54; #允许的ip

add_header 'Access-Control-Allow-Origin' *;//添加响应头header,解决跨域问题,可以放在server中,也可以放在localhost中
}
}
}
 log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                         '$status $body_bytes_sent "$http_referer" '
                         '"$http_user_agent" "$http_x_forwarded_for"'
                         '"$request_body"'
                         '"$upstream_response_time" "$upstream_addr" "$upstream_status" "$request_time"';
    #log_format debug_websocket '$remote_addr - $remote_user [$time_local] "$request" '
    #                              '$status $body_bytes_sent "$http_referer" '
    #                              '"$http_user_agent" "$http_x_forwarded_for" '
    #                              '"$http_upgrade" "$http_connection"';
    access_log  logs/access.log  main;
    access_log  logs/http_access.log  main;

3.2 代理一个虚拟机

server{
		listen 8099;
		server_name 1.117.164.90;
		location / {
			proxy_pass https://123.sogou.com/?121264;

}

3.3 代理集群

upstream mm{
    	server 1.117.164.90:8080;
    	server 1.117.164.91:8080

}
server {
 listen 8099;
		server_name 1.117.164.90;
		location / {
			proxy_pass http://mm;
		}	 
}
 location / {        
 #/代表所有https://baidu.com后面拼接的应用服务都会转发到改server
	proxy_pass https://baidu.com;
    root   html;
    index  index.html index.htm;
}

3.4 负载均衡

#负载均衡策略
# 1 轮询(默认)
# 2 weight
# 3 ip_hash
# 4 least_conn 最少连接方式
# 5 fair(第三方) 响应时间
# 6 url_hash (第三方)

eg:
server 192.168.45.151:8080 weight=2;
server 192.168.45.151:8081 weight=1;

3.5 pid

注意:配置pid,否则多个配置文件,stop,reload都针对是…/logs/nginx.pid中的nginx
pid nginx8099.pid;//会放在logs下
或者
pid /easkapp/opt/openresty/nginx/nginx8099.pid;//绝对路径

3.6 虚拟主机

nginx支持三种类型的虚拟主机配置

  • 基于ip的虚拟主机, (一块主机绑定多个ip地址)
  • 基于域名的虚拟主机(servername)
  • 基于端口的虚拟主机(同一ip不同的端口)

3.7 相同端口号,不同访问域名

https://blog.csdn.net/qq_40737025/article/details/85053164

server {
        listen       82;
        server_name  jieyan22.com;
        location / {
            root   html/chen;
            index  index.html index.htm;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }

server {
        listen       82;
        server_name  yanjie22.com;
        location / {
            root   html/yan;
            index  index.html index.htm;
        }  
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

默认server

  1. 如果配置了default_server,则该server是默认server
  2. 如果没有任何 server 块显式指定 default_server 参数,Nginx 会选择配置文件中第一个匹配指定 listen 端口
server {
   listen 80 default_server;
   server_name _;  # 使用 `_` 作为通配符,表示匹配所有未明确指定的>域名
   # 配置内容...
}

如果没有找到匹配的 server_name,请求会交给默认服务器处理

3.8 一台主机开多个nginx(转发)

 upstream mysvr{
        server localhost:88;
        server localhost:99;


    }
    server {
        listen       84;
        server_name  localhost; 
        location / {
            proxy_pass http://mysvr;
            root   html/88;
            index  index.html index.htm;
        }
        
#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  65;

    

    upstream mysvr{
       # server localhost:88;
        #server localhost:99;
        server  localhost:87;

    }
    server {
        listen       84;
        server_name  localhost; 
        location / {
            proxy_pass http://mysvr;
            root   html/88;
            index  index.html index.htm;
        }

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

      
    }

  server {
        listen       87;
        server_name  jieyan33.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass https://baidu.com;
            root   /usr/local/ex/nginxone/nginxinstall_eight_ssl/conf/html/chen;
            index  index.html index.htm;
        }

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

      
    }

server {
        listen       82;
        server_name  yanjie33.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值