基于域名虚拟主机
[[email protected] nginx]# grep html conf/nginx.conf
root html; #docmont 根目录
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;
# root html;
# root html;
# index index.html index.htm;
# root html;
# index index.html index.htm;
[[email protected] html]# vim index.html
基本于域名的虚拟主机配置
[[email protected] nginx]# tree
.
|-- client_body_temp
|-- conf #配置文件目录
| |-- fastcgi.conf
| |-- fastcgi.conf.default
| |-- fastcgi_params
| |-- fastcgi_params.default
| |-- koi-utf
| |-- koi-win
| |-- mime.types
| |-- mime.types.default
| |-- nginx.conf #主配置文件
| |-- nginx.conf.default
| |-- scgi_params
| |-- scgi_params.default
| |-- uwsgi_params
| |-- uwsgi_params.default
| `-- win-utf
|-- fastcgi_temp
|-- html #默认站点目录,相当于apache htdocs目录
| |-- 50x.html #错误页面
| `-- index.html #首页文件
|-- logs #日志目录
| |-- access.log
| |-- error.log
| `-- nginx.pid
|-- proxy_temp
|-- sbin #启动目录
| `-- nginx
|-- scgi_temp
`-- uwsgi_temp
[[email protected] conf]# egrep -v "#|^$" nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server { #相当于apache vhost
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] conf]# egrep -v "#|^$" nginx.conf >nginx.conf.tmp
[[email protected] conf]# mv nginx.conf.tmp nginx.conf
[[email protected] conf]# cat fastcgi.conf
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param QUERY_STRING $query_string;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param CONTENT_TYPE $content_type;
fastcgi_param CONTENT_LENGTH $content_length;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param REQUEST_URI $request_uri;
fastcgi_param DOCUMENT_URI $document_uri;
fastcgi_param DOCUMENT_ROOT $document_root;
fastcgi_param SERVER_PROTOCOL $server_protocol;
fastcgi_param HTTPS $https if_not_empty;
fastcgi_param GATEWAY_INTERFACE CGI/1.1;
fastcgi_param SERVER_SOFTWARE nginx/$nginx_version;
fastcgi_param REMOTE_ADDR $remote_addr;
fastcgi_param REMOTE_PORT $remote_port;
fastcgi_param SERVER_ADDR $server_addr;
fastcgi_param SERVER_PORT $server_port;
fastcgi_param SERVER_NAME $server_name;
# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param REDIRECT_STATUS 200
[[email protected] nginx]# vim conf/nginx.conf
user nginx nginx; #nginx用户名和组
worker_processes 8; #进程数可以是CPU的数量*2
events {
use epoll; #事件
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.you.com you.com;
location / {
root /data0/www/www;
index index.html index.htm;
}
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] conf]# mkdir /data0/www/{www,bbs,blog} -p
[[email protected] conf]# tree /data0/
/data0/
`-- www
|-- bbs
|-- blog
`-- www
4 directories, 0 files
[[email protected] conf]# chown -R nginx.nginx /data0/www
[[email protected] conf]# chown -R nginx.nginx /data0/www
[[email protected] nginx]# ./sbin/nginx -s reload #平滑启起
基本于多个域名
[[email protected] nginx]# vim conf/nginx.conf
user nginx nginx;
worker_processes 8;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.you.com you.com;
location / {
root /data0/www/www;
index index.html index.htm;
}
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
######
server {
listen 80;
server_name www.bbs.com bbs.com;
location / {
root /data0/www/bbs;
index index.html index.htm;
}
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
######
server {
listen 80;
server_name www.blog.com blog.com;
location / {
root /data0/www/blog;
index index.html index.htm;
}
# error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[[email protected] nginx]# ./sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# ./sbin/nginx -s reload
加日志
[[email protected] nginx]# mkdir -p /app/logs
[[email protected] nginx]# vim conf/nginx.conf
user nginx nginx;
worker_processes 8;
error_log /app/logs/nginx_error.log crit;
pid logs/nginx.pid; #加错误日志
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘; #正确日志
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name www.you.com you.com;
location / {
root /data0/www/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog;
}
}
######
server {
listen 80;
server_name www.bbs.com bbs.com;
location / {
root /data0/www/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
######
server {
listen 80;
server_name www.blog.com blog.com;
location / {
root /data0/www/blog;
index index.html index.html;
access_log /app/logs/blog_access.log commonlog;
}
}
}
service 和配置文件分开
[[email protected] nginx]# mkdir extra
[[email protected] nginx]# cp conf/nginx.conf extra/nginx_vhosts.conf
主配置文件
[[email protected] nginx]# cat conf/nginx.conf
user nginx nginx;
worker_processes 8;
error_log /app/logs/nginx_error.log crit;
pid logs/nginx.pid;
events {
use epoll;
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format commonlog ‘$remote_addr - $remote_user [$time_local] "$request" ‘
‘$status $body_bytes_sent "$http_referer" ‘
‘"$http_user_agent" "$http_x_forwarded_for"‘;
sendfile on;
keepalive_timeout 65;
include /usr/local/nginx/extra/nginx_vhosts.conf; #包含虚拟主机的文件,可以包含多个虚拟主机文件
}
虚拟主机配置
[[email protected] nginx]# cat extra/nginx_vhosts.conf
server {
listen 80;
server_name www.you.com you.com;
location / {
root /data0/www/www;
index index.html index.htm;
access_log /app/logs/www_access.log commonlog;
}
}
######
server {
listen 80;
server_name www.bbs.com bbs.com;
location / {
root /data0/www/bbs;
index index.html index.htm;
access_log /app/logs/bbs_access.log commonlog;
}
}
######
server {
listen 80;
server_name www.blog.com blog.com;
location / {
root /data0/www/blog;
index index.html index.html;
access_log /app/logs/blog_access.log commonlog;
}
}
[[email protected] nginx]# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[[email protected] nginx]# /usr/local/nginx/sbin/nginx -s reload