http{
server {
listen 80;
server_name 域名1;
#项目支持https,如果用http访问导向到https中
rewrite ^(.*)$ https://$host$1 permanent;
}
# 配置项目2
server {
listen 80;
server_name 域名2;
location / {
root C:\\work\\nginx-1.24.0\\html\\项目2地址;
index index.html index.htm;
}
location /api/ {
#需要代理访问的后端服务器地址---后端服务器地址
proxy_pass http://127.0.0.1:79/;
}
}
# HTTPS server
#
server {
listen 443 ssl;
server_name 域名1;
ssl_certificate C:\work\key\xxxx.pem;
ssl_certificate_key C:\work\key\xxxx.key;
location / {
#程序根目录配置
root C:\\work\\nginx-1.24.0\\html\\项目2;
index index.html index.htm;
# 配置所有匹配不到的路径重定向到index.html,vue-router的mode是history模式的情况下需要配置,否则会出现刷新页面404的情况
try_files $uri $uri/ /index.html;
}
location /api/ {
#需要代理访问的后端服务器地址---后端服务器地址
proxy_pass http://127.0.0.1:76/;
}
#配置静态文件
location /public/image/ {
alias C:/work/zhhj/zhhj/public/image/;
}
# 配置访问资源——文件
location /file/ {
alias C:/work/nginx-1.24.0/html/file/;
}
}
}
}
1、其实如果访问项目下面的资源,不需要再nginx配置转发了,可以直接用代理设置
2、配置多个项目,一定要使用多个server服务方式配置,配置一个server是不对
3、捣鼓nginx中,多学了一个小知识,二级域名下可申请多个三级域名,所以完全可以在不暴露IP的情况下访问各个项目










