Linux双网段下通过Docker的Nginx容器部署两个前端Vue项目(根据IP或端口)
lzp1467188465 2024-09-11 08:33:04 阅读 90
业务需求
项目部署过程遇到的一个问题,LINUX服务器上有两个个网口:一个是外网段:192.168.1.1;另一个是内网网段:192.168.3.1,要求两个网段都能访问前端Vue项目。
部署背景
前端项目所在服务器地址IP:192.168.1.137(3.137),Linux默认用的外网网口:1.137。
查看Linux默认网关和网口命令(如需修改默认,可自行百度)
ip route
后端是springcloud框架,gateway服务所在的ip地址和端口:192.168.1.127(3.127)端口:8700
由于前端Vue项目要指定网关gateway的地址,就需要部署两个前端项目
无论是1段还是3段的客户端,通过nginx默认80端口访问前端VUE项目,假设把打包后的VUE项目放在nginx本地(Linux服务器上)
Nginx配置说明
通过server配置块设置请求监听处理,通过server_name配置不同IP,如果请求URL中的服务器IP是3.137则去访问nginx相对路径下html路径(/etc/nginx/html)下的前端项目。
server { -- -->
listen 80;
server_name 192.168.3.137;
client_max_body_size 100m;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#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;
}
如果请求URL中的服务器IP是1.137则去访问/etc/nginx/outer路径下的前端项目,/etc/nginx是docker中nginx容器的默认路径
server {
listen 80;
server_name 192.168.1.137;
client_max_body_size 100m;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /etc/nginx/outer;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
nginx配置
#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 192.168.3.137;
client_max_body_size 100m;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#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;
#}
}
server {
listen 80;
server_name 192.168.1.137;
client_max_body_size 100m;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root /etc/nginx/outer;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
#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 outer;
}
# 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;
#}
}
# 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;
# }
#}
}
docker安装nginx容器命令
### 多路径映射
docker run --name nginx --restart=always -p 80:80 -v /home/deploy/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/deploy/nginx/html:/etc/nginx/html -v /home/deploy/nginx/outer:/etc/nginx/outer -d nginx:1.20.1
声明
本文内容仅代表作者观点,或转载于其他网站,本站不以此文作为商业用途
如有涉及侵权,请联系本站进行删除
转载本站原创文章,请注明来源及作者。