首页 > 系统相关 >Nginx基础 - 03基本配置

Nginx基础 - 03基本配置

时间:2023-03-11 12:36:34浏览次数:37  
标签:03 nginx index 配置 Nginx html conf my root

 

一、 Nginx配置文件结构

Nginx主配置文件/etc/nginx/nginx.conf是一个纯文本类型的文件。 整个配置文件是以区块的形式组织的。一般,每个区块以一对大括号{}来表示开始于结束。 1. Main层位于nginx.conf配置文件的最高层 2. Main层下可以有Event、HTTP层 3. HTTP层下面允许有多个Server层,用于对不同的网站做不同的配置 4. Server层允许有多个Location,用于对不同的路径进行不同模块的配置  
[root@my-node10 nginx]# cat nginx.conf

user  nginx;
worker_processes  auto;

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

    include /etc/nginx/conf.d/*.conf;
}

  

[root@my-node10 conf.d]# cat default.conf
server {
    listen       80;
    server_name  localhost;

    #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;
    }
    # 省略其他的配置
}

  

二、 日志配置

[root@my-node10 nginx]# pwd
/application/nginx
[root@my-node10 nginx]# tree
.
├── hotel
│   └── index.html
└── shop
    └── index.html

2 directories, 2 files

[root@my-node10 conf.d]# cat shop.conf  hotel.conf
server {
    listen 80;
    server_name www.myshop.com;
    root /application/nginx/shop;
    index index.html;
}

server {
    listen 80;
    server_name www.myhotel.com;
    root /application/nginx/hotel;
    index index.html;
}

  

[root@my-node10 conf.d]# nginx -t -c /etc/nginx/nginx.conf
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

[root@my-node10 conf.d]# nginx -s reload

  

访问www.myshop.com 和 www.myhotel.com      

 

192.168.6.102 - - [05/Mar/2023:15:08:59 +0800] "GET / HTTP/1.1" 200 61 "-" 
                                               "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-"
192.168.6.102 - - [05/Mar/2023:15:08:59 +0800] "GET /favicon.ico HTTP/1.1" 404 555 "http://www.myshop.com/" 
                                               "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36" "-"
 

三、Nginx状态监控

--with-http_stub_status_module 记录Nginx客户端访问的状态信息

具体配置:
    location /mystatus {
        stub_status on;
        access_log off;
    }

  

 

  • Active connections: The current number of active client connections including Waiting connections. Nginx当前活跃的连接数
  • accepts: The total number of accepted client connections. 
  • handled: The total number of handled connections. requests: The total number of client requests.
    • Generally, the parameter value is the same as accepts unless some resource limits have been reached (for example, the worker_connections limit).
  • Reading: The current number of connections where nginx is reading the request header.
  • Writing: The current number of connections where nginx is writing the response back to the client.
  • Waiting: The current number of idle client connections waiting for a request.

四、 虚拟主机

虚拟主机,在Web服务器里是一个独立的网站站点,  站点对应独立的域名(也可以是IP或端口), 具有独立的程序及资源目录,独立对外提供服务。   4.1 基于域名的虚拟主机
[root@my-node10 conf.d]# cat hotel.conf
server {
    listen 80;
    server_name www.myhotel.com;
    root /application/nginx/hotel;
    index index.html;
}

[root@my-node10 conf.d]# cat shop.conf
server {
    listen 80;
    server_name www.myshop.com;
    location / {
        root /application/nginx/shop;
        index index.html;
    }
}

  

4.2 配置不同端口访问不同虚拟主机

server {
    listen 8001;
    ......
}

server {
    listen 8002;
    ......
}

  

4.3 配置不同IP访问不同虚拟主机

server {
    listen 80;
    server_name 192.168.6.10;
    ......
}

  

4.4 虚拟主机别名

server {
    listen 80;
    server_name www.myhotel.com myhotel.com myhoteldev.com;
    root /application/nginx/hotel;
    index index.html;
}

 

4.5 文件名的排序

相同的域名, 相同的端口, 谁的文件名在前,谁会被优先读取到。

[root@my-node10 demo]# mkdir  d1 d2 d3
[root@my-node10 demo]# echo "d1-1" > d1/index.html
[root@my-node10 demo]# echo "d2-1" > d2/index.html
[root@my-node10 demo]# echo "d3-1" > d3/index.html
[root@my-node10 demo]# echo "demo" > 1.html
[root@my-node10 demo]# ls
d1  d2  d3  index.html

  

[root@my-node10 conf.d]# cat demo1.conf
server {
    listen 80;
    server_name demo.com;
    root /application/nginx/demo/d1;
    index index.html index.htm;
    location ~ ^/1.html {
        root /application/nginx/demo;
    }
}
[root@my-node10 conf.d]# cat demo2.conf
server {
    listen 80;
    server_name demo.com;
    root /application/nginx/demo/d2;
    index index.html index.htm;
    location ~ ^/1.html {
        root /application/nginx/demo;
    }
}

 

 

[root@my-node10 conf.d]# ll
总用量 28
-rw-r--r-- 1 root root 1152 3月 5 16:21 default.conf
-rw-r--r-- 1 root root 227 3月 5 20:03 demo1.conf
-rw-r--r-- 1 root root 227 3月 5 20:04 demo2.conf

# 访问demo.com, 使用的是 demo1.conf 配置文件
[root@my-node51 ~]# curl http://demo.com
d1-1

 

  

[root@my-node10 conf.d]# mv demo1.conf demo4.conf
[root@my-node10 conf.d]# ll
总用量 28
-rw-r--r-- 1 root root 1152 3月   5 16:21 default.conf
-rw-r--r-- 1 root root  227 3月   5 20:04 demo2.conf
-rw-r--r-- 1 root root  227 3月   5 20:03 demo4.conf

[root@my-node10 conf.d]# nginx -s reload
nginx: [warn] conflicting server name "demo.com" on 0.0.0.0:80, ignored

[root@my-node51 ~]# curl http://demo.com
d2-1
[root@my-node51 ~]# curl http://demo.com/1.html
demo

  

[root@my-node10 conf.d]# cat demo3.conf
server {
    listen 80;
    server_name demo.com default;
    root /application/nginx/demo/d3;
    index index.html index.htm;
    location ~ ^/1.html {
        root /application/nginx/demo;
    }
}

  

[root@my-node51 ~]# curl http://demo.com
d2-1

[root@my-node51 ~]# curl http://demo.com/1.html
demo

 

 

标签:03,nginx,index,配置,Nginx,html,conf,my,root
From: https://www.cnblogs.com/kingdomer/p/13936210.html

相关文章

  • [Go语言Web03]GORM数据库操作
    1.GORM连接MySQL1.1ORM1.1.1什么是ORM1.1.2ORM优缺点优点:提高开发效率缺点:牺牲执行性能牺牲灵活性弱化SQL能力1.2GROM官方文档执行下面命令安装GRO......
  • 解析JAVA环境变量及配置
    写在前面:参考CSDN博主-StandByMeQuan文章:https://blog.csdn.net/qq_37872792/article/details/80642985其实博主已经写得很好了,但是我怕到时后该博文被删了,无法......
  • 2023.03.11.函数重载,引用等
    程序生成的过程:1.预处理:头文件的展开宏的替换预处理指令解析去掉注释2.编译:预处理后文件生成汇编文件.asm(汇编代码)词法解析,语法解析语义分析优化3.汇编:汇编文件进一......
  • Nginx 负载均衡反向代理 Windows安装部署教程
     一、Nginx简介   Nginx(enginex)是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器。它也是一种轻量级的Web服务器,可以作为独立的服务器部署网站......
  • 2023.03.11.命名空间
    c++命名空间为了区分不同库中相同名称的函数、类、变量等命名空间的定义使用关键字namespace,后跟命名空间的名称,如下所示:namespacenamespace_name{//代码声明}为......
  • conda常用命令及配置总结
     一、常用命令 初始化condainit创建环境condacreate-n环境名python=3.x列出所有虚拟环境condaenvlist进入环境condaactivateenv_name退出环......
  • 程序设计应用 2023-03-11
     DjangodoessupporttheModel-View-Controller(MVC)architecturalpattern.However,DjangousesaslightlydifferentapproachcalledModel-View-Template(MV......
  • can't convert cuda:0 device type tensor to numpy. Use Tensor.cpu() to copy the t
     如何解决这个问题:TypeError:can'tconvertcuda:0devicetypetensortonumpy.UseTensor.cpu()tocopythetensortohostmemoryfirst.这个错误通常出现在试......
  • 多媒体技术 2023-03-11
    音频的获取与处理2.1声音的基础知识声音的三个要素是音调、音强和音色。音调是指声音的高低,由频率决定。高频率的声音听起来高,低频率的声音听起来低。音乐中按音阶来表......
  • 总结20230310
    今日上了计算机网络、概率论、实用英语阅读与翻译、web开发技术、数学建模B。计算机网络讲到了第三章--数据链路层;概率论开启了第二章,讲的离散型随机变量;实用英语阅读与......