nginx目录配置详解

进入nginx目录,排除temp文件后,剩余四个目录

[[email protected] nginx]# ls -l | grep -v temp

总用量 36

drwxr-xr-x. 2 root root 4096 7月  12 20:42 conf  → 配置文件

drwxr-xr-x. 2 root root 4096 7月  12 20:42 html  → 站点信息

drwxr-xr-x. 2 root root 4096 7月  12 20:49 logs  → 日志信息

drwxr-xr-x. 2 root root 4096 7月  12 20:42 sbin  → 启动命令

cd /html

里面有文件index.html ,这个文件一般默认是网站的首页

下面进入conf文件夹,下面有一个 nginx.conf 文件

运维有一个很重要的思想叫最小化学习,所以我们要把配置文件简化一下:

简化命令:

egrep -v "#|^$" nginx.conf >> a.conf

^代表行首

$代表行尾

^$意思就是行首之后就是行尾,中间什么也没有,所以代表空行

worker_processes  1;    默认进程有一个

一个worker可以处理多少并发连接

events {

worker_connections  1024;

}

那么有多少个worker呢???可以看配置文件第一行

[[email protected] conf]# ps -ef | grep nginx

root  8398  1     0  Jul12 ?  00:00:00 nginx: master process /app/zpy/nginx/sbin/nginx

zpy   8399  8398  0  Jul12 ?  00:00:00 nginx: worker process

可以看到就一个worker process ,一般认为worker process 与cpu 核数相当

所以nginx最大并发连接数是怎么计算的呢 ??

worker process 乘以  worker_connections

-----------------------

http模块

http {

include       mime.types;

default_type  application/octet-stream;

sendfile       on;

keepalive_timeout  65;

下面这个叫做server标签,一个server标签代表一个虚拟主机

server {

listen       80;            监听端口

server_name  localhost;     域名

location / {                   /  代表直接在浏览器输入 http://10.0.70.3 

root   html;

index  index.html index.htm;    这个代表首页文件

}

error_page   500 502 503 504  /50x.html;

location = /50x.html {    /50.x.html代表直接在浏览输http://10.0.70.3/50.x.html

root   html;

}

nginx 虚拟主机的概念
所谓的虚拟主机,在web服务里就是一个独立的网站站点,这个站点对应独立的域名(也可能是IP或者端口),具有独立程序及资源目录,可以独立的对外提供服务供用户访问

apache是如何定义虚拟主机的 ?

配置文件中 <VirtualHost> </VirtualHost> 内

nginx是如何定义虚拟主机的?

在配置文件中server{} 的标签

虚拟主机一般分为如下三类:

基于域名的虚拟主机

基于端口的虚拟主机

基于ip的虚拟主机

配置基于域名的虚拟主机

server {

listen       80;

server_name  www.vipdailiang.com;

location / {

root   html/www;

index  www.html ;

}

}

1)首先需要在/app/zpy/nginx/html下创建 www目录

mkdir -p /app/zpy/nginx/html/www

2)在www目录下创建www.html,并添加www.zipeiyi.com的内容

echo "www.vipdailiang" >> /app/zpy/nginx/html/www/www.html

3)最重要的一步,nginx一旦应用在企业环境中,它的作用会变得很重要,所以修改了配置文件,不要贸然重启,需要先检查一遍nginx配置文件的合理性

/app/zpy/nginx/sbin/nginx -t

nginx: the configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf syntax is ok

nginx: configuration file /app/zpy/nginx-1.8.1//conf/nginx.conf test is successful

4)平滑重启nginx

/app/zpy/nginx/sbin/nginx -s reload 

备注:

如果nginx参数过多记不住怎么办??

/app/zpy/nginx/sbin/nginx -h

Options:

-?,-h         : this help

-v            : show version and exit

-V            : show version and configure options then exit

-t            : test configuration and exit

-q            : suppress non-error messages during configuration testing

-s signal     : send signal to a master process: stop, quit, reopen, reload

-p prefix     : set prefix path (default: /app/zpy/nginx-1.8.1//)

-c filename   : set configuration file (default: conf/nginx.conf)

-g directives : set global directives out of configuration file

最后在浏览器输入10.0.70.3

但是如果输入:www.vipdailiang.com

这是因为你电脑本机解析不到的原因:

有两种解析的办法:

  1. 修改本机的host文件(linux是/etc/hosts ,windows的C\windows\system32\drivers\etc\hosts)

    添加10.0.70.3 www.vipdailiang.com

  2. 在本地DNS里面加入域名对应关系

最后效果如下,至此基于域名的虚拟主机配置完毕

配置多个基于域名的虚拟主机

server {

listen       80;

server_name  www.vipdailiang.com;

location / {

root   html/www;

index  www.html ;

}

}

server {

listen       80;

server_name  bbs.vipdailiang.com;

location / {

root   html/bbs;

index  bbs.html ;

}

}

这样你就可能会想了,怎么都是80端口,那么我在浏览器输入http://10.0.70.3 会出现什么???

别急,细细道来

我做了一个实验,发现是下图的结构:

如果你用ip进行测试的化,那么应该是按照 server{}的顺序来的

总结:基与域名的虚拟主机,就不要这样测试了,应该用域名测试

到此基于域名的nginx虚拟主机讲解完毕

配置基于端口的虚拟主机

server {

listen       8001;

server_name  www.vipdailiang.com;

location / {

root   html/www;

index  www.html ;   → 这里面内容改为 ism

}

}

server {

listen       8002;

server_name  www.vipdailiang.com;

location / {

root   html/bbs;

index  bbs.html ;  →这里面内容改为imp

}

}

netstat -tunpl | grep nginx ,可以看到nginx起了8001与8002两个端口

tcp  0    0    0.0.0.0:8001   0.0.0.0:*     LISTEN      8398/nginx

tcp  0    0    0.0.0.0:8002   0.0.0.0:*     LISTEN      8398/nginx

那么该怎么检测呢??

curl www.vipdailiang.com:8001

ism

curl www.vipdailiang.com:8002

imp

配置基于IP的虚拟主机

这个需要有多块网卡,nginx服务器需要多个ip

这个在生产环境中极为少见,这里就不做解释了

ok。。至此nginx配置文件就讲解完成了

时间: 2024-08-07 00:11:19

nginx目录配置详解的相关文章

快速部署Python应用:Nginx+uWSGI配置详解

快速部署Python应用:Nginx+uWSGI配置详解 相比于PHP,Python应用的部署很麻烦,比较常用的方法有fcgi与wsgi,然而这两种都很让人头痛.文章介绍了Nginx+uwsgi的简便方法,来快速的部署Python应用. AD: 在PHP里,最方便的就是deployment了,只要把php文件丢到支持PHP的路径里面,然后访问那个路径就能使用了:无论给主机添加多少PHP应用,只要把目录改好就没你的事了,完全不用关心php-cgi运行得如何,deployment极为方便. 反观Py

Nginx 核心配置详解

目录 Nginx 核心配置详解 Nginx 四层访问控制: Nginx账户认证功能: 自定义错误页面: 自定义访问日志: 检测文件是否存在: 长连接配置: 作为下载服务器配置: 作为上传服务器: 其他配置: Nginx 核心配置详解 Nginx 四层访问控制: 准备两个客户端,做访问测试使用. centos7 IP:192.168.39.7 centos6 IP:192.168.39.6 [[email protected] images1]#vim /apps/nginx/conf/conf.

Nginx安装配置详解

1.   Nginx安装 1)下载Nginx: wget http://nginx.org/download/nginx-1.3.11.tar.gz /opt/ 2)安装Nginx: ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_sub_module --with-md5=/usr/lib

Nginx实战配置详解

当单台服务器的负载达到一定程度时,服务器资源就无法满足用户的需求,此时可以通过多种方法来处理.比如说通过DNS解析多台服务器,或者是通过四层根据内容请求进行分发(如LVS),或是通过七层负载技术(nginx.haproxy)等方式来实现.Nginx的反向代理负载均衡能够更好的支持虚拟主机,可配置性强,可以根据服务器的硬件配置按比重进行轮询.权重负载,也可以根据IP哈希.URL哈希对后端服务器做负载,并且还支持对后端服务器的健康检查. 完整的Nginx代理配置如下: user nginx ngin

nginx.conf配置详解

#定义Nginx运行的用户和用户组user www www; #nginx进程数,建议设置为等于CPU总核心数.worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ]error_log /var/log/nginx/error.log info; #进程文件pid /var/run/nginx.pid; #一个nginx进程打开的最多文件描述符数目,理论值应该是最多打开文件数(系统的值ulim

nginx 基础配置详解

#本文只对nginx的最基本配置项做一些解释,对于配置文件拆分管理,更详细的集群健康检查的几种方式,检查策略等在此不做详细解释了. #运行用户user nobody;#启动进程,通常设置成和cpu的数量相等worker_processes 1; #全局错误日志及PID文件#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info; #pid logs/nginx.pid; #工

nginx之nginx.conf配置详解

#配置用户或者组,默认为nobody nobody user nobody; #启动进程,根据硬件调整,通常等于CPU数量或者2倍于CPU worker_processes  1; #指定日志路径,级别.这个设置可以放入全局块,http块,server块,级别以此为:debug|info|notice|warn|error|crit|alert|emerg #error_log  logs/error.log; #error_log  logs/error.log  notice; #error

Nginx配置文件配置详解

http://www.jb51.net/article/69091.htm 详细 http://www.jb51.net/article/72527.htm 包括安装等详细配置 http://blog.csdn.net/tjcyjd/article/details/50695922 更详细 Nginx(engine X) 是一个高性能的 HTTP 服务器和反向代理服务器,这款软件开发的目的是为了解决 C10k 问题. Nginx 的架构利用了许多现代操作系统的特性,以实现一个高性能的 HTTP

nginx location 配置详解

指令作用 匹配指定的请求uri(请求uri不包含查询字符串,如http://localhost:8080/test?id=10,请求uri是/test) 语法形式 location [ = | ~ | ~* | ^~ | @] /uri/ { configuration } 匹配模式及顺序 匹配字符串分为两种:普通字符串(literal string)和正则表达式(regular expression),其中 ~ 和 ~* 用于正则表达式, 其他前缀和无任何前缀都用于普通字符串. 1.先匹配普通