Halo
发布于 2025-01-07 / 6 阅读 / 0 评论 / 0 点赞

nginx按域名转发

安装nginx

apt install -y nginx

同一台主机启动多个网站进程

  1. 启动网站1于8001端口
    http://localhost:8001

  2. 启动网站2于8002端口
    http://localhost:8002

nginx 监听80端口,按域名转发访问

  1. 配置网站1
nano /etc/nginx/conf.d/site1.conf 
server {
    listen 80;
    server_name site1.com;
    location / {
        proxy_pass http://localhost:8001;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}
  1. 配置网站2
nano /etc/nginx/conf.d/site1.conf 
server {
    listen 80;
    server_name site2.com;
    location / {
        proxy_pass http://localhost:8002;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

测试nginx配置是否正确

nginx -t

重启nginx

service nginx restart

评论