首先说说想要实现的效果,我有一个项目部署在服务器的 tomcat 上,假如我服务器的公网 ip 为 40.12.12.12
,
我项目名称是 solo
,我还有一个域名是 javaweb.55555.io
那么,我想实现通过 javaweb.55555.io
就能访问到我的项目,要怎么实现呢
首先是将域名解析到公网 ip 上,这个自行百度,我是用的花生壳,然后就是剩下的两种方式
-
第一种方式:修改 tomcat
* 将tomcat的server.xml中的端口修改为80 ``` <Connector port="80" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> ``` * 在<Host>标签中添加你自己的项目 ``` <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context path="" docBase="solo" reloadable="true" /> <!-- SingleSignOn valve, share authentication between web applications Documentation at: /docs/config/valve.html --> <!-- <Valve className="org.apache.catalina.authenticator.SingleSignOn" /> --> <!-- Access log processes all example. Documentation at: /docs/config/valve.html Note: The pattern used is equivalent to using pattern="common" --> <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" prefix="localhost_access_log" suffix=".txt" pattern="%h %l %u %t "%r" %s %b" /> </Host> ``` 其中的 ```<Context>``` 标签就是你要进行处理的,将path设成空字符串即可
-
第二种方式 利用 nginx 实现转发
-
首先是安装 nginx,可以参考 Centos 安装 nginx ,这里不再赘述
不过要强调一点,如果之前 tomcat 占用了 80 端口,启动 nginx 会报错端口已被占用
或者类似# 98: Address already in use
你需要把 tomcat 端口改回 8080 或者随意什么值,因为默认安装 nginx 配置的是 80 端口 -
好了下面是 最重要的 nginx 配置
-
user root; #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 javaweb.55555.io; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost:8080/; } #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; # } #} }
上面是完整的配置文件,最主的配置在这里
server { listen 80; server_name javaweb.55555.io; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://localhost:8080/; }
当外网访问域名 javaweb.55555.io
时,其实访问的是公网的 80 端口,此时 nginx 监听到了这个端口,
则会将这个请求转到 http://localhost:8080/
上,那么我们的目的就实现了
同样的 tomcat 里同样需要 <Context path="" docBase="solo" reloadable="true" />
配置,
否则就会以这个 javaweb.55555.io/solo
网址访问
如果是 solo 这个开源项目别忘了在 latke.properties 中进行如下配置
# Browser visit protocol serverScheme=http # Browser visit domain name serverHost=javaweb.55555.io # Browser visit port, 80 as usual, THIS IS NOT SERVER LISTEN PORT! serverPort=
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于