nginx的主配置文件为conf/nginx.conf
一、nginx配置文件结构
nginx的配置是以区块型的,大致可分为:全局配置和模块配置
文件中包括
- 全局配置(main)
- 事件模块配置
- HTTP模块配置和其他模块配置
每个模块配置中又分为各个区块,如http模块中有server区块,server有location区块,如果我们把nginx主要用于web服务器的话,则我们主要看http模块
结构如下:
user nobody;
……
events {
events模块配置
}
http {
include mime.types;
……
server {
server_name localhost;
location / {
root html;
}
}
}
二、配置文件参数说明
#user nobody; #使用nginx进程的用户
worker_processes 1; #工作进程数
#error_log logs/error.log; #错误日志文件路径
#error_log logs/error.log notice; #错误日志文件路径及错误日志级别为notice
#error_log logs/error.log info; #错误日志文件路径及错误日志级别为info
#pid logs/nginx.pid; #保存nginx进程号的文件路径
events { #events模块
worker_connections 1024; #工作进程最大连接数
}
http { #http模块开始
include mime.types; #包含mime.types文件(这个文件主要是定义mime文件类型对应一个扩展名)
default_type application/octet-stream; #默认的mime类型
#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; #定义访问日志的路径并指定日志的格式为main
sendfile on; #是否启用sendfile
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65; #连接超时时间设置
#gzip on; #是否启用gzip压缩
server { #虚拟主机段
listen 80; #监听80端口
server_name localhost; #主机名称(域名、IP)
#charset koi8-r; #默认字符编码设置
#access_log logs/host.access.log main;
location / { #当访问地址带有/时
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 { #当访问路径是/50x.html时
root html; #设置根目录
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ .php$ { #当访问路径中包含.php时
# proxy_pass #将代理通道指向 http://127.0.0.1
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ .php$ { #当访问文件中包含.php时
# root html; #设置根目录
# fastcgi_pass 127.0.0.1:9000; #设置fastcgi服务器的地址
# fastcgi_index index.php; #设置fastcgi的索引文件
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name; #设置fastcgi的参数
# include fastcgi_params; #包含文件
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /.ht { #访问文件中包含.ht时
# deny all; #禁止所有问题
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
三、虚拟主机配置:
所有虚拟主机都是在http区域中添加一个server段(区块)
1、基于域名的虚拟主机:
如域名是shixinke.com
server{
server_name shixinke.com;
root /var/www/html; #指定根目录,理论上可以指定任意目录
index index.html; #当直接输入域名,不加任何路径时,会默认显示index.html这个文件
}
2、基于端口的虚拟主机:
如:http://www.shixinke.com:2022
注:首先要判断这个端口是否占用:使用命令netstat tunlp|grep 2022
参数说明:
t表示tcp连接
u表示udp连接
n表示数字
l表示listen,监听
p表示程序名称
server {
listen 2022;
server_name shixinke.com;
root /var/www/html/admin/;
index index.html;
}
3、基于IP的虚拟主机:
如:192.168.18.200
server {
server_name 192.168.18.200;
root /var/www/html;
index index.html;
}