Nginx 学习笔记

常用命令

windows

start nginx: 启动

nginx -s reload: 重启(加载配置)

nginx -t: 测试配置文件

nginx -v: 查看版本信息

nginx -c 配置文件路径: 指定配置文件

nginx -s quit: 退出(保存相关信息)

nginx -s stop: 停止(不保存相关信息)

配置信息

虚拟主机配置


#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  0;
    keepalive_timeout  65;

    #gzip  on;

    # 配置虚拟
    
    #主机1
    server {

        # 监听端口
        listen       80;
        # 访问的地址
        server_name  a.project.com;

        # 日志存放地址
        access_log  /data/logs/nginx/a.project.com.log  main;
        

        location / {
            # 网站根目录
            root   /data/site/a.project.com;
            # 默认页面
            index index.html;
        }
    }

    #主机2
    server {

        # 监听端口
        listen       80;
        # 访问的地址
        server_name  b.project.com;

        # 日志存放地址
        access_log  /data/logs/nginx/b.project.com.log  main;
        
        location / {
            # 网站根目录
            root   /data/site/b.project.com;
            # 默认页面
            index index.html;
            # 开启目录浏览(如果默认的页面不存在)
            autoindex on;
            # 显示文件简略大小(可读性好)
            autoindex_exact_size off;
            # 显示时间(服务器时间)
            autoindex_localtime on;
        }
    }
}

目录结构:

D:\DATA
├─logs
│  └─nginx
└─site
    ├─a.project.com
    └─b.project.com

a.project.com 跟 b.project.com 两个文件夹下都有一个 index.html 文件,如果想看autoindex on命令的效果,把 B 目录下的 index.html 文件删掉即可。

动态代理配置

    # 配置负载均衡
    
    # 代理主机
    upstream projectcom {
        # 服务器地址
        server 127.0.0.1:8081;
        server 127.0.0.1:8082;
        # 策略[ip_hash | fair | url_hash]
    }

    #主机
    server {

        # 监听端口
        listen       80;
        # 访问的地址
        server_name  a.project.com;

        # 日志存放地址
        access_log  /data/logs/nginx/a.project.com.log  main;
        

        location / {
            # 网站根目录
            root   /data/site/a.project.com;
            # 默认页面
            index index.html;
        }

        # 配置代理,以 .action 结尾的请求交由代理处理
        location ~ .*.action$ {
            proxy_pass http://projectcom;
        }
    }

后台开启两台 tomcat 服务器,分别监听 8081,8082 端口。 都提供一个 .action 接口,分别响应不同内容(这里是为了区分,实际场景一样),如 demo.action,通过 http://localhost/demo.action 请求。

Nginx 学习笔记_第1张图片
具体配置选项

你可能感兴趣的:(Nginx 学习笔记)