Flask - nginx

nginx的特点

  1. 轻量级
  2. 并发能力强
  3. 高度模块化
  4. 负载均衡
  5. 反向代理

正向代理和反向代理

https://www.cnblogs.com/taostaryu/p/10547132.html

正向代理

  • 正向代理类似一个跳板机, 代理访问外部资源.
  • 比如我们在国内访问不到谷歌, 可以通过正向代理访问
  • 隐藏了真实的请求客户端,服务端不知道真实的客户端是谁

用途

  • 访问原来无法访问的资源
  • 缓存, 加速访问资源
  • 对客户端访问进行认证
  • 隐藏了用户的资料

反向代理

  • 反向代理的运行方式 : 以代理服务器来接收internet上的连接请求, 然后将请求转发给内部网络上的服务器, 并将从服务器上得到的结果返回给internet上请求连接的客户端.

作用

  • 负载均衡.

安装nginx

  • 包管理工具安装
  1. 安装先决条件 : sudo yum install yum-utils
  2. 创建目录 : touch /etc/yum.repos.d/nginx.repo
  3. 把以下代码放到文件里
[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
  1. 使用nginx主线包 : sudo yum-config-manager --enable nginx-mainline
  2. 安装nginx : sudo yum install nginx
  3. 当提示接受GPG密钥时,验证指纹是否与573B FD6B 3D8F BC64 1079 A6AB ABF5 BD82 7BD9 BF62匹配,如果匹配则接受。

启动nginx

  • 启动 : systemctl start nginx
  • 查看进程 : ps -aux | group nginx

查看配置文件

user  nginx;  ## 用户叫nginx
worker_processes  1;  ## worker进程一个

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


events {
    worker_connections  1024;
}


http {
	# 标识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;
}

/etc/nginx/conf.d/default.conf

server {	
	# 默认监听的是80端口
    listen       80;
    # 服务器
    server_name  localhost;

    #charset koi8-r;
    #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;
    }

nginx命令

  • 启动nginx : systemctl start nginx
  • 关闭nginx服务 : systemctl stop nginx
  • 设置开机自启 : systemctl enable nginx
  • 查看任务 : ps -aux | grep nginx

部署静态页面

  • 创建 nginx.conf文件

user  root;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
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;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /root/myproject/2048;
        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;
    }  
}   
}
  • 启动 : nginx -c 默认配置目录
    • 这里是nginx -c /root/myproject/2048/nginx.conf
  • 关闭 : nginx -s signal stop
  • 重启 : nginx -s signal reload
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值