centos 实现 Apache 与 Ngnix 并存同时正常访问(配制域名转发)


关键词:域名、转发、反向代理、端口

1. 背景简介

只有一台VPS主机服务器,但想同时部署两个网站:
通过访问IP想打开一个页面,
访问已购买的域名想打开另一个页面。

2. 软件安装,与配置

Apache、Ngnix服务正常安装
其中Apache的监听端口改为:Listen 8080,(vim /etc/httpd/conf/httpd.conf
其中Ngnix的监听端口改为:listen 8888,(vim /etc/nginx/nginx.conf
重启服务

systemctl restart nginx.service
systemctl restart httpd.service 

3. Ngnix服务端转发(域名,IP等)

Ngnix转发Apache配置:

vim /etc/nginx/conf.d/test.conf

加入

server {
        # 监听80端口,一般不用改
    listen 80;
        # 将这里的example换成自己的域名
    server_name 118.89.194.65;

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 这里的http后面的url就是你访问你上方的域名代理的地址
        proxy_pass http://127.0.0.1:8080/;
    }
}

server {
        # 监听80端口,一般不用改
    listen 80;
        # 将这里的example换成自己的域名
    server_name lastinvent.online www.lastinvent.online *.lastinvent.online;

    location / {
        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        # 这里的http后面的url就是你访问你上方的域名代理的地址
        proxy_pass http://127.0.0.1:8888/;
    }

最后,配置完成,再次重启服务


附:备用方案(暂时未用上)

参考:
Apache服务端转发(域名,IP等)

vim /etc/httpd/conf.d/httpdvhosts.conf

加入

<VirtualHost *:80>
  ServerName localhost
  # 设置访问转发的域名
  ServerAlias xxxx.com

  ProxyPreserveHost On
  ProxyRequests Off

  # 代理重定向地址
  # (内网IP加端口 = 你的服务)
  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
</VirtualHost>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值