环境描述
用django搭建简单web服务器
客户端-------(192.168.200.241)N ---------(192.168.200.233)django
新建appb
urls.py
urlpatterns = [
path(’’,views.index),
path(‘index.htm’,views.index),
re_path(r’^.*’,views.Publish),
]
views.py
def index(request):
return HttpResponse(“index.htm for appb.”)
def Publish(request):
url = request.get_full_path()
host = request.get_host()
return HttpResponse(str(“You are visit for :http://”) + host + url)
测试:
nginx端用tcpdump 查看 host和uri
#tcpdump -A -i ens192 -n ‘host 192.168.200.233 and (((ip[2:2] - ((ip[0]&0xf)<<2)) - ((tcp[12]&0xf0)>>2)) != 0)’
初步试错
NGINX 配置
server {
listen 80;
server_name localhost;
location /appb {
proxy_pass http://192.168.200.233;
}
}
IP 192.168.200.241.32856 > 192.168.200.233.http:
GET /appbskfen.htm HTTP/1.0
Host: 192.168.200.233
IP 192.168.200.233.http > 192.168.200.241
HTTP/1.0 404 Not Found
Server: gunicorn
server {
listen 80;
server_name localhost;
location /appb/ {
proxy_pass http://192.168.200.233;
}
}
再改改,但是粗心,改错app。
server {
listen 80;
server_name localhost;
location /appb/ {
proxy_pass http://192.168.200.233/appa;
}
}
IP 192.168.200.241.32864 > 192.168.200.233.http
GET /appakent.htm HTTP/1.0
Host: 192.168.200.233
IP 192.168.200.233.http > 192.168.200.241
HTTP/1.0 404 Not Found
Server: gunicorn
加个斜杠/
server {
listen 80;
server_name localhost;
location /appb/ {
proxy_pass http://192.168.200.233/appa/;
}
IP 192.168.200.241.32866 > 192.168.200.233.http
GET /appa/kent.htm HTTP/1.0
Host: 192.168.200.233
改正确
server {
listen 80;
server_name localhost;
location /appb/ {
proxy_pass http://192.168.200.233/appb/;
}
}
补充:
server {
listen 80;
server_name localhost;
location /appb { proxy_pass http://192.168.200.233/appb/; }
}
star@STAR-LEE:~$ curl http://192.168.200.241/appbkt.htm
You are visiting: http://192.168.200.233/appb/kt.htm
IP 192.168.200.241.32962 > 192.168.200.233.http
GET /appb/kt.htm HTTP/1.0
Host: 192.168.200.233
location | proxy_pass | GET URI from browser | TO webserver |
---|---|---|---|
/appb | ip | /appbskfen.htm | /appbskfen.htm |
/appb/ | ip | /appb/skfen.htm | /appb/skfen.htm |
/appb/ | ip/appa | /appb/ken.htm | /appaken.htm |
/appb/ | ip/appa/ | /appb/ken.htm | /appa/ken.htm |
/appb/ | ip/appb | /appb/te.htm | /appbte.htm |
/appb | ip/appb/ | /appbkt.htm | /appb/kt.htm |
/appb/ | ip/appb/ | /appb/ken.htm | /appb/ken.htm |
2022年01月11日
补充:
django
appa/urls.py
urlpatterns = [
path('',views.index),
path('index.htm',views.index),
path('test1.htm',views.test1),
re_path(r'^.*',views.Publish),
]
appa/views.py
def index(request):
return HttpResponse("index.htm for appa.")
def test1(request):
return HttpResponse("test1 in appa.")
def Publish(request):
url = request.get_full_path()
host = request.get_host()
return HttpResponse(str("You are visiting (appa) for :http://") + host + url)`
nginx
server {
listen 80;
location / { return 404 "error";}
location /appa/ { proxy_pass http://192.168.200.112:3001/appa/;}
location /appb { proxy_pass http://192.168.200.112:3001/appb/;}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
有两个斜杠;
改成:
location /appb/ { proxy_pass http://192.168.200.112:3001/appb;}
URI in browser | locatioin | proxy_pass | after nginx |
---|---|---|---|
/appb/skdf.htm | /appb/ | /appb | /appbskdf.htm |
/appb/skdf.htm | /appb/ | /appb/ | /appb/skdf.htm |
/appb/skdf.htm | /appb | /appb | /appb/skdf.htm |
/appb/skdf.htm | /appb | /appb/ | /appb//skdf.htm |