Nginx核心配置文件介绍
  Kdni1C6EUdDC 2023年11月02日 56 0

  本文我们来介绍下Nginx的核心配置文件nginx.conf

Nginx的核心配置文件

Nginx.conf的位置: /usr/local/nginx/conf/nginx.conf
  默认的Nginx服务器配置文件都存放在安装目录的conf中,主要的配置文件名为nginx.conf.
  Nginx的核心模块有是Main和Events,还包括Http模块、邮件模块、还可以支持第三方模块等

Nginx核心配置文件介绍_配置文件

注意:
1.每行配置的结尾需要加上分号
2.如果配置项值中包括语法符号,比如空格符,那么需要使用单引号或双引号括住配置项值,否则Nginx会报语法错误
3.“#”注释符
4.单位简写
当指定空间大小时,可以使用的单位包括:
·K或者k千字节(KiloByte,KB)。
·M或者m兆字节(MegaByte,MB)。
例如:
gzip_buffers 4 8k; client_max_body_size 64M;
当指定时间时,可以使用的单位包括:
·ms(毫秒),s(秒),m(分钟),h(小时),d(天),w(周,包含7天), M(月,包含30天),y(年,包含365天)。
例如:
expires 10y;
proxy_read_timeout 600;
client_body_timeout 2m;

main段

# 用于设置master进程启动后,fork出的worker进程运行在哪个用户和用户组下
#user nobody;
# 指定工作衍生进程数(一般等于CPU的总核数或总核数的两倍,两个四个CPU,就设置8)
worker_processes 1;
# 指定错误日志存放的路基,错误日志记录级别可选 [debug | info | notice | warn | error | crit ]
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
# 指定 pid 存放的路径
#pid logs/nginx.pid;

# 指定文件描述符数量
worker_rlimit_nofile 512000;

events段

events {
# 使用的网络I/O模型,Linux推荐使用epoll模式 FreeBSD推荐使用kqueue模型
use epoll;
# 单个woker进程支持的最大连接数
worker_connections 1024;
}

http段

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;

## 定义作为web服务器的相关属性 可以有多个
server {
# 监听的端口
listen 80;
# 服务名称
server_name localhost;
# 字符集
#charset koi8-r;

#access_log logs/host.access.log main;
# 定义一个虚拟主机的属性,所有的web服务必须定义成一个虚拟主机
location / {
# 资源存放的根目录在 html文件夹下
root 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 html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

Nginx的虚拟主机配置

  了解了nginx.conf核心配置文件中基础的内容后,我们来看看这样一个需求,比如说我们现在要搭建三个服务,分别是bbs服务,门户系统和公司内部系统,这时我们可以通过三个web服务来搭建,但为了方便我们可以利用nginx的虚拟主机来实现这三个服务,具体怎么做呢?如下:
Nginx核心配置文件介绍_nginx_02
1.首先在nginx的根目录下创建三个文件夹:bbs,edu,www

Nginx核心配置文件介绍_配置文件_03
2.分别在这三个文件夹中放入对应的资源文件,我们随便放入一个html页面即可(能区别即可)
Nginx核心配置文件介绍_html_04
3.修改nginx.conf配置文件,添加三个server配置

server {
listen 80;
# 虚拟主机配置
server_name bbs.gupao.com;

#charset koi8-r;

#access_log logs/host.access.log main;
# 虚拟主机对应的服务的资源地址
root /usr/local/nginx/bbs;
location / {
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 html;
}
}

4.修改客户的host文件
Nginx核心配置文件介绍_html_05

5.启动服务,测试

Nginx核心配置文件介绍_html_06

Nginx核心配置文件介绍_nginx_07

Nginx核心配置文件介绍_nginx_08
Nginx核心配置文件介绍_nginx_09


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

Kdni1C6EUdDC