Nginx配置虚拟主机

【Nginx配置虚拟主机】标签(空格分隔): nginx
1 新增域名为 $host 静态文件服务器 1.1 打开 Nginx 配置文件 /usr/local/nginx/conf/nginx.conf, 在 http 范围引入虚拟主机配置文件如下:

[root@localhost conf]# vim /usr/local/nginx/conf/nginx.conf # 引入虚拟服务器配置文件 include conf/conf.d/*.conf;

1.2 进入 /usr/local/nginx/conf 目录,创建虚拟主机配置文件目录 conf.d
[root@localhost conf]# cd /usr/local/nginx/conf [root@localhost conf]# mkdir conf.d

1.3 添加配置文件
新建配置文件 "$host".conf
[root@localhost conf]# touch$host.conf

内容如下:
server { listen80; server_name$host; access_loglog/$host.access.log; error_loglog/$host.error.log; root/var/www/$host; location / { indexindex.html index.htm; }}

1.4 重载 Nginx 服务
[root@localhost conf]# nginx -s reload

2 让 Nginx 虚拟主机支持 PHP 在前面第 2 步的虚拟主机服务对应的目录加入对 PHP 的支持。
server { listen80; server_namedemo.local; access_loglog/demo.local.access.log; error_loglog/demo.local.error.log; root/var/www/demo.local; location / { indexindex.html index.htm index.php; }# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 location ~ \.php$ { fastcgi_pass127.0.0.1:9000; fastcgi_indexindex.php; includefastcgi.conf; } }

3 添加图片防盗链功能 图片作为重要的耗流量大的静态资源, 可能网站主并不希望其他网站直接引用, Nginx可以通过 referer 来防止外站盗链图片.
server { listen80; server_namedemo.local; access_loglog/demo.local.access.log; error_loglog/demo.local.error.log; root/var/www/demo_neoease_com; location / { indexindex.html index.htm index.php; }# 这里为图片添加为期 1 年的过期时间, 并且禁止 Google, 百度和本站之外的网站引用图片 location ~ .*\.(ico|jpg|jpeg|png|gif)$ { expires 1y; valid_referers none blocked demo.local*.google.com*.baidu.com; if ($invalid_referer) { return 404; } } }

    推荐阅读