现在有两个前端app-H5项目,共用了一个pc端后台接口,简单的方法是配两个端口访问不同的前端项目,但是客户服务器只有一个80端口开放,于是乎研究了下一个端口下nginx部署多个前端项目的方法,这个功能类似于tomcat,在tomcat下是可以放多个项目的,但是tomcat没有反向代理功能,所以淘汰了tomcat使用nginx会比较方便。
capture.png如上图在nginx服务器中有两个前端项目 一个在默认的html文件夹下的index.html,一个在gtmcApp文件夹下的index.html
编辑conf文件夹下的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;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
location /api/ {
#root gtmcApp;
#index index.html index.htm;
proxy_pass http://172.16.4.120:8580/;
#允许cros跨域访问
#add_header 'Access-Control-Allow-Origin' '*';
}
location /gtmcApp {
alias gtmcApp;
index index.html index.htm;
}
location /gtmcApp/api/ {
#root gtmcApp;
#index index.html index.htm;
proxy_pass http://172.16.4.120:8580/;
#允许cros跨域访问
#add_header 'Access-Control-Allow-Origin' '*';
}
#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;
# }
#}
}
如上面配置,在80端口下配置两个location,一个是根目录,通过配置root来指定html文件夹(默认就是这个),一个是/gtmcApp目录,通过配置alias gtmcApp;
来实现gmtcApp文件夹的识别,每个server只能有一个root 所以在根目录默认有root之后,可以通过alias来配置其他文件夹,其他两个location共用的同一个后台,通过/api/
和/gtmcApp/api/
来区分
如此,localhost访问的根目录进入一个前端项目 localhost/gtmcApp进入第二个前端项目便配置好了。
网友评论