系统:windows 8
需求:希望在服务器上部署多个web app。
启动多个Django,每个进程占用一个端口,访问时需要写:IP + 端口号。但其实不想记端口。
或者这种需求:
一个WebAPP是JSP写的,放到了tomcat上,一个WebAPP是Python写的,放到了Django上,一个WebAPP是ASP写的,放到了IIS上。
更希望看到的是,url1访问APP1,url2访问APP2,url3访问APP3。
更极端的情况是,只能由某一个端口通过http协议访问这台服务器。
解决:
所谓的反向代理(隐藏服务器端)
Django两个应用:A,B
用一个Nginx作为请求转发的服务器,根据不同的url,分发到不同的web app去。
下载、解压(nginx-1.13.11)
下载地址:NGINX下载链接
一点都不大, 才4MB不到。
相关操作
坑:
1. 首先要start nginx.exe
2. 可能会启动多个,自己看进程数量(大于2)知道了。由于2,会导致3无效。
3. 改了配置文件,需要reload。但如果在step 2中启动了多个,谁知道你的reload命令发给了nginx的哪个进程。
浏览器上,输入 http://localhost , 表示nginx启动成功。
修改配置文件 nginx.conf
#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 80; # nginx的端口 server_name localhost; # 访问nginx的IP(域名),如果有公网IP或域名,可以填公网的 # charset koi8-r; #access_log logs/host.access.log main; location / { # 这是根目录。浏览器上打 localhost,就可以看到nginx的欢迎页面了。 root html; index index.html index.htm; } location /a { # localhost/a 即可访问下面的那个网址 proxy_pass http://localhost:8088/mobile/all_products; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } location /ab { # localhost/ab 即可访问下面的那个网址 proxy_pass http://localhost:8089/mobile/order_preview; proxy_redirect off; proxy_set_header X-Real-IP $remote_addr; #后端的Web服务器可以通过X-Forwarded-For获取用户真实IP proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } #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; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
反向代理成功
其中一个 web app
另一个 web app
Ref. 官网:Nginx for Windows
后记
为什么写这一篇。
实验室有个网站,有个公网可以访问的域名(链接:实验室网站)
有个内网IP,绑了个复旦的域名,现在的情况是我拿这个内网IP接了个无线路由器。
我在路由器设置页面中,用NAT端口映射到了tomcat,tomcat上跑的是实验室网站。
但是我现在还想在上面部署别的web应用,然而是这个应用是用python的django开发的,所以…