nginx添加虚拟主机vhost
在nginx的conf目录里新建vhosts目录
然后在nginx.conf的http{ }代码段内添加
include vhosts/*.conf;
添加普通主机
在vhosts目录里新建一个conf文件,比如 test.com.conf
server
{
listen 80;
server_name test.com www.test.com;
index index.php index.html index.htm default.html ;
root D:/wwwroot/test.com;
#PHP解析代码开始,如果没有PHP可以不需要这一段
location ~ \.php(.*)$ {
try_files $uri =404;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
set $real_script_name $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
set $real_script_name $1;
set $path_info $2;
}
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
include fastcgi_params;
}
#PHP解析代码结束
access_log D:/NPM/nginx/logs/test.com.log;
error_log D:/NPM/nginx/logs/test.com.log;
}
更详细的可以在server段内添加如下代码
#ERROR-PAGE-START 错误页配置,可以注释、删除或修改
error_page 404 /404.html;
error_page 502 /502.html;
#ERROR-PAGE-END
#禁止访问的文件或目录
location ~ ^/(\.user.ini|\.htaccess|\.git|\.svn|\.project|LICENSE|README.md)
{
return 404;
}
#一键申请SSL证书验证目录相关设置
location ~ \.well-known{
allow all;
}
#设置附件缓存时间和禁止附件日志
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
error_log /dev/null;
access_log /dev/null;
}
#设置js和csss缓存时间和禁止js和css日志
location ~ .*\.(js|css)?$
{
expires 12h;
error_log /dev/null;
access_log /dev/null;
}
添加https主机
server
{
listen 80;
listen 443 ssl http2;
server_name test.com www.test.com;
index index.php index.html index.htm default.html ;
root D:/wwwroot/test.com;
#SSL-START SSL相关配置,
ssl_certificate d:/NMP/nginx/ssl/test.com.pem;
ssl_certificate_key d:/NMP/nginx/ssl//test.com.key;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_ciphers EECDH+CHACHA20:EECDH+CHACHA20-draft:EECDH+AES128:RSA+AES128:EECDH+AES256:RSA+AES256:EECDH+3DES:RSA+3DES:!MD5;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;
ssl_session_timeout 10m;
add_header Strict-Transport-Security "max-age=31536000";
error_page 497 https://$host$request_uri;
#SSL-END
access_log D:/NPM/nginx/logs/test.com.log;
error_log D:/NPM/nginx/logs/test.com.log;
}
如果需要PHP解析请添加上PHP解析代码
添加反向代理主机
server{
listen 80;
server_name test.com www.test.com;
location / {
proxy_set_header Accept-Encoding '';
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header From-Https on;
proxy_pass http://127.0.0.1:80;
}
}
proxy_pass只需要用IP就行,不一定要用域名,被代理的主机上绑定域名test.com就行,如果代理多个域名甚至可以直接把server_name改成 *.com或者*.cn代表代理目标服务器的所有com和cn域名,前提是目标主机绑定的才能生效