Spring boot Nginx、spring boot hello world和example.com
根据本指南: 我已经安装了nginx并启动了springboot helloworld应用程序。但当我在浏览器中访问example.com时,它只显示: 如果我直接访问它,请访问: http://localhost:8080/Spring boot Nginx、spring boot hello world和example.com,spring-boot,nginx,Spring Boot,Nginx,根据本指南: 我已经安装了nginx并启动了springboot helloworld应用程序。但当我在浏览器中访问example.com时,它只显示: 如果我直接访问它,请访问: http://localhost:8080/ Hello world 它起作用了 我错过了什么 详情: /etc/nginx/conf.d/helloworld.conf server { listen 80; listen [::]:80; server_na
Hello world
它起作用了
我错过了什么
详情:
/etc/nginx/conf.d/helloworld.conf
server {
listen 80;
listen [::]:80;
server_name example.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}
/etc/systemd/system/helloworld.service
[Unit]
Description=Spring Boot HelloWorld
After=syslog.target
After=network.target[Service]
User=root
Type=simple
[Service]
ExecStart=/home/user/.sdkman/candidates/java/11.0.10-open/bin/java -jar /home/user/repos/hello-world/build/libs/hello-world-0.0.1-SNAPSHOT.jar
Restart=always
StandardOutput=syslog
StandardError=syslog
SyslogIdentifier=helloworld
[Install]
WantedBy=multi-user.target
并且应用程序正在运行:
$ sudo systemctl status helloworld
● helloworld.service - Spring Boot HelloWorld
Loaded: loaded (/etc/systemd/system/helloworld.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2021-03-13 03:23:21 CET; 6s ago
Main PID: 1277942 (java)
Tasks: 41 (limit: 47589)
Memory: 244.4M
CGroup: /system.slice/helloworld.service
我还尝试过:teeest0001.com(因为下面的答案中指出example.com是一个公共/保留名称):
随后是一个
systemctl守护进程重新加载
,但会导致服务器未找到错误。example.com
是一个实际的域,但根据它的定义,它是保留的供示例使用
如果您查找example.com
的DNS记录,您可以看到它解析为真实的IP地址,而不是您的服务器:
~λnslookup example.com
服务器:10.50.100.3
地址:10.50.100.3#53
非权威性回答:
名称:example.com
地址:93.184.216.34
最简单的方法就是在主机文件中添加一个条目,将example.com
重定向到本地服务器
条目的外观如下所示:
127.0.0.1 example.com
添加它之后,您将能够点击example.com:8080
,它将点击您的开发服务器。如果你想在没有端口的情况下点击example.com
,你需要让你的开发服务器监听端口80,这需要在运行它时使用sudo
,这可能不是一个好主意。您还可以使用socat
或类似或的反向代理
需要注意的是,如果您希望其他人访问该站点,这将只在您修改主机文件的计算机上起作用,这有点超出了本问题的范围,但这些链接可能会有所帮助:
- (此答案以Windows服务器为例,但步骤基本正确)
example.com
的保留性质,而是您的机器没有将example.com
或teeest0001.com
翻译到您的本地机器
服务器未找到错误
是因为没有teeest0001.com
的DNS记录
关于使用localhost时连接所涉及的内容,如果您连接到端口8080,则您将绕过nginx,但是如果您使用端口80,则nginx正在侦听该端口,然后将使用ProxyPass连接到Spring启动应用程序。我不确定是否理解。在上面的例子中,我使用的是在我的机器上本地运行的nginx,所以重点是我应该能够连接到example.com,它应该点击nginx,然后发送到localhost。如果我添加到/etc/hosts,我基本上会使用locahost的另一个名称,这意味着nginx不会出现在图片中?已删除,将更新答案,因为问题已更新。
server {
listen 80;
listen [::]:80;
server_name teeeest0001.com;
location / {
proxy_pass http://localhost:8080/;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Port $server_port;
}
}