美文网首页
Nginx/Apache反向代理

Nginx/Apache反向代理

作者: 流过 | 来源:发表于2018-07-24 11:06 被阅读0次

Nginx

1.下载Nginx
  • 注意不要直接双击nginx.exe,这样会导致修改配置后重启、停止nginx无效,需要手动关闭任务管理器内的所有nginx进程
2.在nginx.exe目录,打开命令行工具,用命令 启动/关闭/重启nginx

start nginx : 启动nginx
nginx -s reload :修改配置后重新加载生效
nginx -s reopen :重新打开日志文件
nginx -t -c /path/to/nginx.conf 测试nginx配置文件是否正确

关闭nginx:
nginx -s stop :快速停止nginx
nginx -s quit :完整有序的停止nginx

image

在浏览器窗口输入localhost出现如图所示页面,即为代理成功

image

打开config文件夹下 nginx.conf文件

image
3.在http下添加
server {
    listen 80;
    server_name www.dyk.com;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:8082;
    }
}

这样,在访问www.dyk.com的时候就会被Nginx代理到http://127.0.0.1:8082端口上

Apache(使用phpstudy工具)

  • 打开vhost-conf文件
image
  • 在文件中添加以下代码,即可将www.dyk.com指向任意文件
<VirtualHost *:80>
    DocumentRoot "E:\WWW/test/child/pro.html"
    ServerName www.dyk.com
    ServerAlias dyk.com
  <Directory "E:\WWW/test/child/pro.html">
      Options FollowSymLinks ExecCGI
      AllowOverride All
      Order allow,deny
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

  • 代理端口,首先引入下图中Apache模块
image
  • vhost-conf文件中添加以下代码,即可将www.dyk.com指向任意端口
<VirtualHost *:80>
    ServerName www.dyk.com
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
ProxyPass / http://127.0.0.1:8080/
ProxyPassReverse / http://127.0.0.1:8080/
</VirtualHost>

补充:如果同学们想在本地测试,可以设置几个拦截域名

  • 找到hosts文件
image
  • 添加如下内容
127.0.0.1    www.veblen1.com
127.0.0.1    www.veblen2.com

相关文章

网友评论

      本文标题:Nginx/Apache反向代理

      本文链接:https://www.haomeiwen.com/subject/srxemftx.html