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;
}
}
}
推荐阅读
- vue-cli|vue-cli 3.x vue.config.js 配置
- 从战略性的角度可以配置股票
- 缓存有关的配置和属性
- Spring|Spring Boot 自动配置的原理、核心注解以及利用自动配置实现了自定义 Starter 组件
- 监控nginx
- Vagrant|Vagrant (三) - 网络配置
- java|java b2b2c shop 多用户商城系统源码- config 修改配置
- CentOS7 阿里云镜像配置方法
- Python|Win10下 Python开发环境搭建(PyCharm + Anaconda) && 环境变量配置 && 常用工具安装配置
- 为Google|为Google Cloud配置深度学习环境(CUDA、cuDNN、Tensorflow2、VScode远程ssh等)