使用nginx来代理一个服务器下的不同项目
前言:上一篇文章中,我实现了在同一tomcat下部署不同的项目,通过IP+指定端口可以访问到各自项目,那么新的需求来了:使用IP+端口太麻烦,而且这样的URL(例如作为扫码跳转地址时)在微信浏览器中,会出现这样的提示:
这样用户体验不好,敏感信息也多,如何通过域名,来访问这同一服务器下的一个tomcat中部署的两个项目,而且做到访问如丝般润滑呢?是时候祭出Nginx了
1.安装nginx:
安装和配置途中也遇到不少的坑,具体的安装可以参考这篇文章,非常感谢这位博主:Linux下nginx的安装以及环境配置
2.配置域名
安装好nginx之后,再来明确一下甲方需求:需要用不同的域名,来访问这一台服务器下,同一个tomcat中部署的两个项目,先在心中默念三遍,再开始配置:
1.登陆阿里云,进入域名解析控制台,我现在有一个一级域名:weige.com,点击设置
点击添加记录,设置内容
配置信息
如此这般,我便配置好了第一个域名:jintian.weige.com---------->192.163.0.2(我的服务器地址),如法炮制,第二个域名:mingtian.weige.com----->192.163.0.2(我的服务器地址)
3.修改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; #监听80端口
server_name jitian.weige.com; #第一个服务名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://jitian.weige.com:8099;#第一个服务转发的路径
}
#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; #默认也监听80端口
server_name mingtian.weige.com;#第二个服务名
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
proxy_pass http://mingtian.weige.com:8299;#第二个服务转发的路径
}
#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;
# }
#}
}
nginx的默认端口是80,我们这里不做修改,我们解析的域名也默认访问的是80端口,这样,当使用域名请求服务器时,请求会先到达nginx,nginx根据不同的服务名,将请求转发到不同的项目,我的理解是这样,若有不对,欢迎吐槽。
4.重启nginx
保存配置文件,重启nginx服务器,进入到安装路径下,我这里是:/usr/local/nginx/sbin,
执行命令:./nginx
查看进程:ps -ef |grep nginx,若出现类似提示,说明启动成功:
访问第一个项目jintian.weige.com:
访问第二个项目:mingtian.weige.com:
测试成功,流程若有不对的,欢迎指正。