需求
最近用微擎框架做微信公众号开发,需要通过绑定站点域名来收发信息。因此,平常在本地的开发环境是不能够满足需求的。但是,我们不可能每更改一点东西就将更改之后的内容更新到服务器上面去测试吧。为了能够让 URL 访问到本地的服务器,可以将指定域名的访问反向代理到本地来。
解决
| 名称 | 协议 | 外部端口 | 内部 IP 地址 | 内部端口 |
|——–|———|—————|——————–|—————|
| lizs | TCP 和 UDP | 8112 | 192.168.1.99 | 80 |
- 在远程服务器上面配置一个方向代理,指向你本地生产环境的服务器。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| server {
listen 80;
server_name *.lizsblog.com;
location / {
proxy_redirect off;
proxy_pass http://183.26.116.104:8112;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 20m;
}
}
|
proxy_pass
参数后面的 183.26.116.104
是你本地网络的 IP,可以通过百度搜索 ip 即可查看;8112
是你本地网络给你 PC 设置的虚拟服务器的端口号。
- 通过从远程服务器中反向代理你本地的 PC 之后,只要配置本地 Nginx 服务器就可以了。
Nginx 一般都有一个 default
配置的,里面的 root
只要指向你的项目文件就可以了。比如,
1 2
| root /home/lizs/app/we7/shengmingtrip
|
但是,这样存在一个问题,当你要用同样的方法做第二个项目的时候,你就要将 root
指向你的第二个项目文件目录了。那么,如果想要访问第一个项目呢?不是又要修改回来了吗?那如果有更多的项目呢?我们可以根据不同的域名来访问不同的项目文件:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
| # Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
set $dir_name default;
# shengming project
if ($host = "shengmingtrip.lizs.easecloud.cn") {
set $dir_name we7/shengmingtrip;
}
# dsyd project
if ($host = "dsyd.lizs.easecloud.cn") {
set $dir_name we7/dsyd;
}
root /home/lizs/app/$dir_name;
# Add index.php to the list if you are using PHP
index index.html index.htm index.php index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php5-cgi alone:
fastcgi_pass 127.0.0.1:9000;
# With php5-fpm:
# fastcgi_pass unix:/var/run/php5-fpm.sock;
}
}
|