Nginx/Apache反向代理
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Nginx/Apache反向代理相关的知识,希望对你有一定的参考价值。
初衷:一台服务器上只有一个80端口,如果要部署多套网站,难免会有www.veblen.com:80808080或者www.veblen.com/test/child/pro.html这样的尴尬情况,反人类的操作。
nginx
- 下载Nginx
- 双击应用程序即可启动
![技术分享](http://upload-images.jianshu.io/upload_images/7687940-35836439d99199dd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
n3.png
-
在浏览器窗口输入localhost出现如图所示页面,即为代理成功
n2.png - 打开config文件夹下
nginx.conf
文件
![技术分享](http://upload-images.jianshu.io/upload_images/7687940-40fc80be996f0399.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
n1.png
在http下添加
server {
listen 80;
server_name www.veblen.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.veblen.com的时候就会被Nginx代理到http://127.0.0.1:8082端口上
Apache(使用phpstudy工具)
- 打开
vhost-conf
文件
![技术分享](http://upload-images.jianshu.io/upload_images/7687940-44ee4bc3f3359d8c.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
a1.png
- 在文件中添加以下代码,即可将www.veblen.com指向任意文件
<VirtualHost *:80>
DocumentRoot "E:\WWW/test/child/pro.html"
ServerName www.veblen.com
ServerAlias veblen.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模块
![技术分享](http://upload-images.jianshu.io/upload_images/7687940-c4a0d1c34d1971e1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
a2.png
- 在
vhost-conf
文件中添加以下代码,即可将www.veblen.com指向任意端口
<VirtualHost *:80>
ServerName www.veblen.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文件
![技术分享](http://upload-images.jianshu.io/upload_images/7687940-2c42c237edce19ab.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
h1.png
添加如下内容
127.0.0.1 www.veblen1.com
127.0.0.1 www.veblen2.com
以上是关于Nginx/Apache反向代理的主要内容,如果未能解决你的问题,请参考以下文章
Nginx+apache/Tomcat实现反向代理与动静分离
Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第三部分
Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分