昨天试着自己搭建了一个ngrok服务. 因为80端口被nginx占用了, 只能带端口号访问, 很不方便, 就想着用nginx代理一下
首先, 当然是启动ngrok服务端和客户端, 然后配置nginx
我用的是一个server块里配置多个域名, 配置如下, 访问 test.aaa.site 就行了, 需要注意的是, 如果使用nginx代理, 则客户端配置的子域名需要与nginx中配置的一致, 且域名需要解析到服务器上, 这样在访问的时候才可以, 实际上就是nginx监听wechat.yourdomain.site的80端口, ngrok监听wechat.yourdomain.site的8888端口, nginx接收到请求之后转发给ngrok,相当于访问了wechat.yourdomain.site:8888链接
server {
listen 80;
server_name www.yourdomain.site aaa.site *.aaa.site;
#charset koi8-r;
#access_log logs/host.access.log main;
#微信公众号测试
location /wx {
proxy_pass http://127.0.0.1:8888/;
#Proxy Settings
proxy_redirect off;
#proxy_set_header Host downloads.openwrt.org;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /app {
root html;
proxy_pass http://127.0.0.1:9696/;
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 100m;
index index.html index.htm;
}
location / {
root html;
proxy_pass http://127.0.0.1:8080/;
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 100m;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
proxy_pass http://127.0.0.1:8888/;
#Proxy Settings
proxy_redirect off;
#proxy_set_header Host downloads.openwrt.org;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
location /app {
root html;
proxy_pass http://127.0.0.1:9696/;
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 100m;
index index.html index.htm;
}
location / {
root html;
proxy_pass http://127.0.0.1:8080/;
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 100m;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
也可以把ngrok的子域名单独放一个server块里, 如果这样配置的话, 用ip访问的话会出现 Tunnel 47.96.9.213:8888 not found 的提示, 这时只要把你默认的server块里的 listen 端口号后面加上 default 就可以了, listen 80 default;
server {
listen 80;
server_name test.aaa.site;
#charset koi8-r;
#access_log logs/host.access.log main;
#微信公众号测试
location / {
proxy_pass http://127.0.0.1:8888;
#Proxy Settings
proxy_redirect off;
#proxy_set_header Host downloads.openwrt.org;
proxy_set_header Host $host:8888;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_max_temp_file_size 0;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
}