java 如何使用嵌入式tomcat在Spring Boot App中设置域名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46398695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set Domain name in Spring Boot App with embedded tomcat
提问by Nikesh Kedlaya
I am developing a application in spring boot with embedded tomcat. In my local which runs on a port 8080 and i can give url http://locahost:8080
. How can change this to my domain? like www.mydomain.com
which should work similar to localhost. How to configure this? I am using embedded tomacat not externally installed tomcat server.
我正在使用嵌入式 tomcat 在 spring boot 中开发一个应用程序。在我的本地运行在端口 8080 上,我可以提供 url http://locahost:8080
。如何将其更改为我的域?就像www.mydomain.com
它应该像本地主机一样工作。这个怎么配置?我使用的是嵌入式 tomacat,而不是外部安装的 tomcat 服务器。
回答by shazin
First of all you need to have a domain registered.
首先,您需要注册一个域。
Then you need to have a Machine in premise or in the Cloud whose Public IP address is mapped to the domain you registered, and that has the correct port (80) opened.
然后,您需要在内部或云中拥有一台机器,其公共 IP 地址映射到您注册的域,并且打开了正确的端口 (80)。
Then you need to start your Spring boot application to run on port 80 not 8080. You can do that by using CLI argument --server.port=80
or adding server.port=80
in application.properties
file or application.yaml
file.
然后,你需要启动你的春天启动的应用程序端口80上没有运行8080您可以通过使用命令行参数做,--server.port=80
或添加server.port=80
在application.properties
文件或application.yaml
文件。
回答by ennth
If you are deploying this spring boot application as your primaryservice and not running it on a server that has Apache Web Server already installed, you can manually set the port 80, which is for HTTPrequests. 443 is encrypted and thus HTTPS. You can set these settings on your firewall of your server.
如果您将此 Spring Boot 应用程序部署为主要服务,而不是在已安装 Apache Web Server 的服务器上运行它,您可以手动设置端口 80,该端口用于HTTP请求。443 是加密的,因此是HTTPS。您可以在服务器的防火墙上设置这些设置。
However, if this Spring boot app happens to be something like an API, where it's just endpoints you want to hit from a website that you have on your server (running on something like an Apache Web Server), you're going to need to setup up a reverse proxy or else they will both be trying to use port 80:
但是,如果这个 Spring Boot 应用程序恰好是一个 API 之类的东西,它只是您想从服务器上的网站访问的端点(在 Apache Web 服务器之类的东西上运行),您将需要设置反向代理,否则他们都会尝试使用端口 80:
So you should leave the port as 8080 on the Spring app (running the embedded tomcat server), and your Apache Web Server should be using port 80 for say , your website at www.mydomain.com.
因此,您应该将 Spring 应用程序(运行嵌入式 tomcat 服务器)上的端口保留为 8080,并且您的 Apache Web 服务器应该使用端口 80,例如您的网站 www.mydomain.com。
Thus, the proxy will redirect incoming HTTP requests to your Tomcat service at port 8080 and thus the endpoints will be triggered via www.mydomain.com/api-end-point-here
因此,代理会将传入的 HTTP 请求重定向到端口 8080 处的 Tomcat 服务,因此端点将通过 www.mydomain.com/api-end-point-here 触发
回答by tryingToLearn
You don't have to specify the domain name anywhere in your application.
您不必在应用程序中的任何位置指定域名。
In the SpringBoot projectr open the application.properties
file(under src/main/resources
)
在 SpringBoot 投影仪中打开application.properties
文件(下src/main/resources
)
And configure the port on which you want to run your application using
并使用配置要在其上运行应用程序的端口
server.port = XXXX
where XXXX is the port number.(80 if you don't want to provide the port while accessing the application)
其中 XXXX 是端口号。(如果您不想在访问应用程序时提供端口,则为 80)
The only extra configuration that needs to be done is to update DNS to point mydomain.com to IP address of your machine. For now, since you are using your local machine, you can test whether redirect works by editing your hosts file (C:\Windows\System32\drivers\etc
)
to keep this mapping.
唯一需要做的额外配置是更新 DNS 以将 mydomain.com 指向您机器的 IP 地址。现在,由于您使用的是本地计算机,您可以通过编辑主机文件 ( C:\Windows\System32\drivers\etc
) 来测试重定向是否有效以保留此映射。
NOTE: This editing will enable you to test only if you are accessing the domain from your machine only.
注意:此编辑将使您能够仅在您仅从您的机器访问域时进行测试。