nginx基本概念及应用

1、nginx简介

2、nginx安装

  • 安装需要的pcre、openssl、zlib、gcc依赖
  • 安装nginx
    解压安装命令:
tar -xvf xxx
./configure
make && make install

linux需要开放访问端口

3、nginx常用命令

  • 查看nginx版本
./nginx -v
  • 启动nginx
./nginx
  • 停止nginx
./nginx -s stop
  • 重新加载配置
./nginx -s reload

4、nginx的配置文件

  • nginx配置文件位置
    /usr/local/nginx/conf/nginx.conf
  • 内容:

5、配置实例

  • 反向代理
server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            proxy_pass http://127.0.0.1:8080;
        }
}

即使用localhost:80访问 / 时会被转发到 http://127.0.0.1:8080

  • 负载均衡
http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    upstream myserver {
        server 127.0.0.1:8080;
        server 127.0.0.1:8081;
        ip_hash;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            proxy_pass http://myserver;
        }
    }
}

即使用localhost:80访问 / 的请求会被平均分配到127.0.0.1:8080和127.0.0.1:8081。

posted @ 2020-10-21 21:52  luotuoccc  阅读(119)  评论(0)    收藏  举报