nginx 基本配置

nginx 基本配置

Nginx主配置⽂件 /etc/nginx/nginx.conf。配置文件已区块形式存在,每个区块以⼀对⼤括号 {} 来表示开始与结束。

  • 1.Main位于nginx.conf配置⽂件的最⾼层
  • 2.Main层下可以有Event、HTTP层
  • 3.HTTP层下⾯有允许有多个 Server层 , ⽤于对不同的⽹站做不同的配置
  • 4.Server层也允许有多个Location, ⽤于对不同的路径进⾏不同模块的配置

MAIN

位于nginx.conf 的配置文件最高层,一般配置全局通用配置

user //设置nginx服务的系统使⽤⽤户
worker_processes //⼯作进程, 配置和CPU个数保持⼀致
error_log //错误⽇志, 后⾯接⼊的是路径
pid //Nginx服务启动时的pid

EVENTS

事件模块,通常配置nginx所使用的通信模型,最大连接数等参数,该配置同时生效当前nginx下所有配置站点


events { //事件模块
 worker_connections //每个worker进程⽀持的最⼤连接数
 use //内核模型,select,poll,epoll
}

http

配置nginx 站点

http {
... 
 //必须使⽤虚拟机配置站点, 每个虚拟机使⽤⼀个server{}段
 'server' {
 listen 80; //监听端⼝, 默认80
 server_name localhost; //提供服务的域名或主机名
 //控制⽹站访问路径
 'location' / {
 root /usr/share/nginx/html; //存放⽹站路径
 index index.html index.htm; //默认访问⾸⻚⽂件
vx: WingspanGo
vx: WingspanGo
Nginx虚拟主机
所谓虚拟主机,在web服务器⾥是⼀个独⽴的⽹站站点,这个站点对应独⽴的域名(也可能是IP或端⼝),具有独⽴
的程序及资源⽬录,可以独⽴地对外提供服务供⽤户访问。
配置基于域名虚拟主机
配置不同端⼝访问不同虚拟主机
 }
 //指定错误代码, 统⼀定义错误⻚⾯, 错误代码重定向到新的Locaiton
 error_page 500 502 503 504 /50x.html;
 'location' = /50x.html {
 root html;
 }
 }
 ...
 //第⼆个虚拟主机配置
 'server' {
 ...
 }
}

虚拟主机

虚拟主机指在web 服务器中的独立网站站点,这个站点对应独⽴的域名(也可能是IP或端⼝),具有独⽴的程序及资源⽬录,可以独⽴地对外提供服务供⽤户访问。

配置基于域名的虚拟主机

1.创建web站点⽬录
[root@LNMP conf]# mkdir /soft/code/{www,bbs}
[root@LNMP conf]# echo "www.wingsredevsecops.top" > /soft/code/www/index.html
[root@LNMP conf]# echo "bbs.wingsredevsecops.top" > /soft/code/bbs/index.html
2.配置虚拟主机
[root@LNMP conf]# cat conf.d/{www,bbs}.conf
server {
 listen 80;
 server_name www.wingsredevsecops.top;
 root /soft/code/www;
}
server {
 listen 80;
 server_name bbs.wingsredevsecops.top;
 root /soft/code/bbs;
}

配置基于不同端口的虚拟主机

mkdir -p /soft/code/800{1..2}
echo "8001" > /soft/code/8001/index.html
echo "8002" > /soft/code/8002/index.html

//仅修改listen监听端⼝即可, 但不能和系统端⼝发⽣冲突
[root@180-143 conf.d]# cat 800*
server {
 listen 8001;
 root /soft/code/8001;
 index index.html index.htm;
}
server {
 listen 8002;
 root /soft/code/8002;
 index index.html index.htm;
}
posted @ 2024-04-08 10:26  DreamDZhu  阅读(10)  评论(0编辑  收藏  举报