初衷:一台服务器上只有一个80端口,如果要部署多套网站,难免会有www.veblen.com:80808080或者www.veblen.com/test/child/pro.html这样的尴尬情况,反人类的操作。
Nginx
- 下载Nginx
- 注意不要直接双击nginx.exe,这样会导致修改配置后重启、停止nginx无效,需要手动关闭任务管理器内的所有nginx进程
在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

-
在浏览器窗口输入localhost出现如图所示页面,即为代理成功
n2.png
- 打开config文件夹下
nginx.conf
文件

在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
文件

- 在文件中添加以下代码,即可将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模块

- 在
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文件

添加如下内容
127.0.0.1 www.veblen1.com
127.0.0.1 www.veblen2.com
欢迎光临个人站清风笔记
网友评论
proxy_set_header Host $http_host;
这两句话,找了一下午,终于找到了,表示感谢