美文网首页
Nginx的配置备忘

Nginx的配置备忘

作者: JohnYuCN | 来源:发表于2020-06-07 07:50 被阅读0次

一、安装:

1、mac/Ubuntu安装

# mac安装
homebrew install nginx
# linux安装
sudo apt-get install nginx
# 查看配置文件位置
nginx -V

2. 管理

# 启动
nginx #或者:brew services start nginx / systemctl start nginx
# 停止
nginx -s stop #或者:brew services stop nginx / systemctl stop nginx

# 重新加载配置
nginx -s reload

# 重新启动
nginx -s reopen #或者: brew services restart nginx / systemctl restart nginx

3. 配置文件编辑:

# 查看配置文件位置
nginx -V

# 编辑配置文件:
vim /etc/nginx/nginx.conf
# 或者:
vim /usr/local/etc/nginx/nginx.conf

二、http server的使用

以下,我复制并简化了配置文件,同时使用了复本配置文件进行了启动

nginx $ cat nginx1.conf 
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    server {
        listen       8200;
        server_name  localhost;
        location / {
             root   /var/www/html;
             index  index.html index.htm;
        }
    }
    include servers/*;
}
# 启动:
$ nginx -c /usr/local/etc/nginx/nginx1.conf

三、使用docker 完成反向代理和负载均衡

0. 实验环境准备:

为配合以上实验,配置了docker-compose.yaml
配置了三个容器:

  • nginx : 用来完成反向代理,负载均衡的测试。
  • busybox1,busybox2: 用来完成nginx 的代理对象。
version: "3"
services:
  nginx:
    image: nginx
    container_name: nginx
    ports:
    - 80:80
    volumes:
    - ./nginx.conf:/etc/nginx/nginx.conf
    - ./html:/usr/share/nginx/html
  busybox1:
    image: busybox
    container_name: busybox1
    expose:
    - 80
    volumes:
    - ./htdoc/busybox1:/var/www
    command: httpd -f -h /var/www
  busybox2:
    image: busybox
    container_name: busybox2
    expose:
    - 80
    volumes:
    - ./htdoc/busybox2:/var/www
    command: httpd -f -h /var/www

1. 网站虚拟主机的配置

文件:/etc/nginx/nginx.conf,配置了两台主机。

worker_processes  2;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;
    server {
        listen  80;
        server_name localhost;
        location / {
            root    /usr/share/nginx/html/localhost;
            index   index.html  index.html;
        }
    }
    server {
        listen  80;
        server_name     ali005;
        location / {
            root    /usr/share/nginx/html/ali005;
            index   index.html      index.html;
        }
    }
}

2. 反向代理的配置

localhost和ali005分别指到了不同的服务器

worker_processes  2;

events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;
    server {
        listen  80;
        server_name localhost;
        location / {
            proxy_pass http://busybox1:80;
            index   index.html  index.html;
        }
    }
    server {
        listen  80;
        server_name     ali005;
        location / {
            proxy_pass http://busybox2:80;
            index   index.html      index.html;
        }
    }
}

3. 负载均衡的配置

worker_processes  2;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    #tcp_nopush     on;
    upstream myapp1{
        server busybox1:80 weight=10;
        server busybox2:80 weight=10;
    }
    server {
        listen  80;
        server_name localhost;
        location / {
            proxy_pass http://myapp1;
            index   index.html  index.html;
        }
    }
}

相关文章

网友评论

      本文标题:Nginx的配置备忘

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