Nginx

作者: 奥特曼255 | 来源:发表于2018-05-28 14:16 被阅读0次

参考资料

tengine 团队教程

主要用途

  1. 反向代理
  2. 负载均衡
    目前支持4种调度算法:
  • weight 轮询(默认),可设置权值 weight
  • ip_hash, 同一个 ip 可由同一个服务器去处理,适合缓存
  • fair, 根据各台服务器的响应处理时间去分配
  • url_hash,

单台服务器并发量:
一般 qps 平均为 50ms(可使用听云监控) ,则单个进程每秒可并发 20 条请求。则一台 8 核的服务器可并发 20 * 17 = 340 条请求。
以上为理想情况。实际情况下,假设一家初创公司,有 10 万注册用户,有两台处理业务的服务器足够了,并发数一般在几百。

  1. 静态资源服务器
# nginx 使用(还有 Apache,是和 nginx 一样流行的服务器软件)
# ====

有了 gunicorn 为什么要用 nginx(理由)
1,Nginx 市场占有率高,开发投入大,安全性高, bug 修复快
2,Nginx 可以配置静态文件读取,增加网站效率
3,Nginx 可以启动多个 gunicorn 实例,并都绑定在 80 端口 

# 安装 nginx
sudo apt-get install nginx

# nginx 常用命令
sudo service nginx restart

# restart 有时候会没效果, 这时候就先 stop 再 start
# 或者重启服务器再试试
sudo service nginx stop
sudo service nginx start

简写为下面这句
# service nginx {start|stop|status|restart|reload|configtest} 


nginx 反向代理配置(什么是反向代理)
# 删掉默认网站配置
sudo rm /etc/nginx/sites-enabled/default
# 增加自己的网站, demo 可以是任意你想要的名字
sudo nano /etc/nginx/sites-enabled/demo

# # 开头的行是注释, 注意行尾的分号
# 文件开始, proxy_pass 后面是 gunicorn 的端口
server {
    listen 80;
    location / {
        proxy_pass http://localhost:8000;
    }
}
# 文件结束


静态资源配置(为什么)
server {
    listen 80;
    location /static {
        alias /项目路径/static;
    }
    location / {
        proxy_pass http://localhost:8000;
    }
}

权限问题
路径问题



# 通用 wsgi.py 文件
# (wsgi 是什么)

#!/usr/bin/env python3

import sys
from os.path import abspath
from os.path import dirname

sys.path.insert(0, abspath(dirname(__file__)))


import app
application = app.configured_app()





# 重启服务器的命令可以写成脚本
# (什么是脚本, 怎么写, 有什么用)



# 用 git 管理代码




# 服务器管理
HTTP 服务器
    nginx
    apache

Web 服务器
    gunicorn

守护进程(监控程序)
    systemd
    supervisor

推荐 gunicorn + nginx + systemd
流行的管理软件是 supervisor, 但是不如 systemd 好用

基础配置

server {
    listen 80;
    server_name buy.aoteman.shop;    # 反向代理设置限定域名
    location / {
        proxy_pass http://localhost:8001;
    }
}

gua提供的方案

一台机器用 nginx 部署 2 个 gunicorn 程序
两种方案

方案 1
在同一个域名下, 配置如下, 需要在程序中改变程序的路由
server {
    listen 80;
    location /app1 {
        proxy_pass http://localhost:8000;
        proxy_redirect     off;
        proxy_set_header   Host $host;
    }
    location /app2 {
        proxy_pass http://localhost:8001;
        proxy_redirect     off;
        proxy_set_header   Host $host;
    }
    location /static {
        alias /home/web14/static;
    }
}

方案 2
用两个不同的子域名来部署
配置暂时我还没做过, 你可以自行搜索一番

gongting 提供的方案

浏览器默认访问80端口,可是服务器的80端口就一个,这就需要我们把其他端口上的web应用,代理到80端口上
nginx的安装在这里也不赘述,配置文件放在/etc/nginx/site-available/下。每次创建一个配置,都要在site-enable下创建一个软连接

首先 sudo service nginx start,然后访问服务器的ip,看到了nginx的欢迎页,说明成功了。

拷贝一份default,命名为little_server(或者干脆新建一个,干净清爽),输入以下配置

server {
    listen 80;
    server_name 0x01.pw www.0x01.pw;

    location / {
        proxy_pass http:///localhost:5000/;
        proxy_read_timeout 120;
        proxy_redirect off;
        proxy_set_header Host $http_host;
                proxy_set_header Remote-Addr $http_remote_addr;
                proxy_set_header X-Real-IP $remote_addr;
        access_log /var/log/nginx/little-server.log;
        error_log /var/log/nginx/little-error.log;
    }
}

这里省略了很多的http header 的转发
执行 sudo nginx -t && service nginx reload, 这个是安全启动,如果测试不通过,不会reload。

Nginx 反向代理

1.安装 NGINX $ apt-get install nginx
2.创建 NGINX 的配置文件 nano /etc/nginx/conf.d/bigc.conf Ctrl + O 保存;Ctrl + X 退出

# 配置 bigc.cc
server {
    listen 443;
    server_name bigc.cc;
    # SSL证书
    ssl on;
    ssl_certificate conf.d/1_bigc.cc_bundle.crt;
    ssl_certificate_key conf.d/2_bigc.cc.key;
    ssl_session_timeout 5m;
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
    ssl_prefer_server_ciphers on;
    location / {
        # 代理地址
        proxy_pass http://127.0.0.1:1207/;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_redirect     off;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
}
# 重定向
server {
    listen 80;
    rewrite ^(.*) https://$host$1 permanent;
}
# 配置 me.bigc.cc
server {
    listen 80;
    # 多个 name 以空格分隔
    server_name me.bigc.cc;
    location / {
        # 代理地址
        proxy_pass http://127.0.0.1:8081/;
        proxy_read_timeout 300;
        proxy_connect_timeout 300;
        proxy_redirect     off;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   Host              $http_host;
        proxy_set_header   X-Real-IP         $remote_addr;
    }
}
# 你皮任你皮 把当你瓜皮

3.nginx -t 检查无错后,重启nginx nginx -s reload

相关文章

网友评论

    本文标题:Nginx

    本文链接:https://www.haomeiwen.com/subject/ybhgjftx.html