零:配置虚拟主机有三种方法:
1、基于域名:相同端口,不同域名
server {
listen 80;
server_name www.linuxidc.com;
index index.html;
root /data/www;
}
server {
listen 80;
server_name bbs.linuxidc.com;
index index.html;
root /data/bbs;
}
2、基于端口:相同域名,不同端口
server
{
listen 8000;
server_name www.linuxidc.com;
root /data/www;
}
server
{
listen 8001;
server_name www.linuxidc.com;
root /data/bbs;
}
3、基于IP:端口、域名一样,IP不一样(这种情况只用于内部网络,不应用于互联网)
server
{
listen 192.168.20.20:80;
server_name www.linuxidc.com;
root /data/www;
}
server
{
listen 192.168.20.21:80;
server_name www.linuxidc.com;
root /data/bbs;
}
一、增加html目录
[root@First ~]# mkdir /usr/local/nginx/topunixhtml
二、配置nginx 配置文件——基本配置
1、原配置文件中已经有了http的server内容,http是主配置模块。server是虚拟主机的模块。
worker_processes 1;
events {
worker_connections 1024;
}
http { ###http是全局块,每个server需要放在http{}下
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#include /usr/local/nginx/conf/vhost/*.conf;
server { ####server 是一个虚拟主机配置
listen 8888;
server_name 域名.com;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
#error_page 404 /404.html;
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#解析.php的文件
location ~ \.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
}
2、增加虚拟主机——实现其他页面访问
(1)静态页面——增加http的server
server {
listen 8888;
server_name www.topunix.com;#通过域名访问,可以解析到此虚拟主机的配置
location / {
root topunixhtml; #这是相对路径,也可以写为绝对路径:/usr/local/nginx/topunixhtml/
index index.html index.htm;
}
把index.html文件放到topunixhtml下,重新加载nginx,并打开网址:www.topunix.com
[root@First topunixhtml]# /usr/local/nginx/sbin/nginx -s reload


(2)动态页面(php等)——在增加——生效后打开www.topunix.com/index.php
<1>在nginx.conf下增加如location ~ \.php$ {...}等内容
server {
listen 8888;
server_name www.topunix.com;
location / {
root /usr/local/nginx/topunixhtml;
index index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/nginx/topunixhtml;
}
location ~ \.php$ {
root /usr/local/nginx/topunixhtml;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/topunixhtml/$fastcgi_script_name;
include fastcgi_params;
}
<2> 在/usr/local/nginx/topunixhtml/下,增加index.php
<?php echo "who are you ?";
<3>加载nginx配置
[root@First conf]# /usr/local/nginx/sbin/nginx -s reload
并查看网站:www.topunix.com/index.php,可以看到php已经生效

3、实现文件下载(去掉动态PHP内容,只保留静态的,既可以实现文件共享、下载)在location增加对应参数
server {
listen 8888;
server_name www.topunix.com;
location / {
#root topunixhtml;
#index index.html index.htm;
autoindex on; ##显示索引
autoindex_exact_size on; ##显示大小
autoindex_localtime on; ##显示时间
sendfile on; ##启用sendfile指令可消除将数据复制到缓冲区的步骤,并允许将数据从一个文件描述符直接复制到另一个文件描述符
sendfile_max_chunk 1m; ##为了防止一个快速连接完全占用工作进程,可以使用sendfile_max_chunk指令来限制单个调用中传输的数据量
tcp_nopush on; ## 将tcp_nopush指令与发送文件指令一起使用。这使 NGINX 能够在 获取 数据块后立即在一个数据包中发送 HTTP 响应标头
tcp_nodelay on; ## 允许关闭Nagle的算法 . 现在,当提供大型静态文件时,无论数据包大小如何,都可以立即发送数据。
keepalive_timeout 65;
}
}
三、生效配置
[root@First conf]# /usr/local/nginx/sbin/nginx -s reload
文章评论