Nginx reverse proxy springboot jar package source code analysis
样例一:
server { listen 80; server_name 127.0.0.1; access_log logs/book.log; error_log logs/book.error; #将/wx-service请求转发给http://127.0.0.1:8011/wx-service处理 location /wx-service { proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8011/wx-service; } #将/bootdo请求转发给http://127.0.0.1:8012/bootdo处理 location /bootdo { proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8012/bootdo; } #将/xcloud-service请求转发给http://127.0.0.1:8013/xcloud-api处理 location /xcloud-service { proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:8013/xcloud-api; } #将/eureka-service请求转发给http://127.0.0.1:8081/eureka-service处理 location /eureka-service { proxy_pass http://127.0.0.1:8081/eureka-service; #这里的端口记得改成项目对应的哦 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; } #将/xcloud-api请求转发给http://127.0.0.1:8082/xcloud-api处理 location /xcloud-api { proxy_pass http://127.0.0.1:8082/xcloud-api; #这里的端口记得改成项目对应的哦 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; } }
样例二:
server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm index.php; } #将/wvv请求转发给http://127.0.0.1:1992/wvv处理 location /wvv { proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:1992/wvv; } }
如果是部署多个springboot项目,则可以多次添加如下配置,只要修改成不一样的路径即可
#将/wvv请求转发给http://127.0.0.1:1991/project处理
location /project{ proxy_set_header host $host; proxy_set_header x-forwarded-for $proxy_add_x_forwarded_for; proxy_pass http://127.0.0.1:1991/project; }
将nginx的端口改成80端口 域名就可以直接访问 service_name可以为localhost 代表本机 localtion /xxx这块就是配置nginx端口转发 不输了几个项目就配置几个 也可以将tomcat中的项目配置在这 也是没问题的 根据上述配置 在sbin目录执行 ./nginx -s reload 刷新nginx配置就会生效了
在这提供一个spring-boot快速重启shell脚本 亲测有效
export java_home=/usr/local/java/jdk1.8.0_162 export path=$java_home/bin:$path export classpath=.:$java_home/lib/dt.jar:$java_home/lib/tools.jar port=8081 jarname=clouddo-server.jar logspatch=./logs_$port id=`ps -ef | grep $port | grep -v "grep" | awk '{print $2}'` echo $id echo "---------------" for id in $id do kill -s 9 $id echo "killed $id" done echo "---------------" rm -rf $logspatch mkdir $logspatch export.utf-8 set -m nohup java -jar -dlogging.path=$logspatch $jarname>$logspatch/catlina.out 2>&1 & tail -f $logspatch/catlina.out
保存命名为xx.sh
建议在nginx下建立一个单独的文件夹以项目名命名,然后将jar包放入其中,再启动jar包。
java -jar revenue-1.0.jar >revenue.txt &
记住 springboot项目得在配置文件中配置
server:
context-path: /xcloud-api
spring boot默认是/ 这样直接通过http://ip:port/就可以访问到index页面 但是我们要通过nginx配置多项目的话就要给每一个项目单独指定context-path
在服务器目录根据个人喜好新建一个文件夹 专门用来存放spring-boot打包成的jar 和重启脚本 类似这样
这样方便管理 logs目录是启动脚本之后生成的日志文件夹 忽略 一个项目对应一个文件夹 里面包含项目jar和一个重启shell脚本
这样就可以同时后台启动多个springboot项目并且通过一个域名来访问这些项目 如果要实时查看日志 请进入到每个项目文件的logs-目录执行
The above is the detailed content of Nginx reverse proxy springboot jar package source code analysis. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to configure Nginx in Windows? Install Nginx and create a virtual host configuration. Modify the main configuration file and include the virtual host configuration. Start or reload Nginx. Test the configuration and view the website. Selectively enable SSL and configure SSL certificates. Selectively set the firewall to allow port 80 and 443 traffic.

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to confirm whether Nginx is started: 1. Use the command line: systemctl status nginx (Linux/Unix), netstat -ano | findstr 80 (Windows); 2. Check whether port 80 is open; 3. Check the Nginx startup message in the system log; 4. Use third-party tools, such as Nagios, Zabbix, and Icinga.

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

The methods that can query the Nginx version are: use the nginx -v command; view the version directive in the nginx.conf file; open the Nginx error page and view the page title.

How to configure an Nginx domain name on a cloud server: Create an A record pointing to the public IP address of the cloud server. Add virtual host blocks in the Nginx configuration file, specifying the listening port, domain name, and website root directory. Restart Nginx to apply the changes. Access the domain name test configuration. Other notes: Install the SSL certificate to enable HTTPS, ensure that the firewall allows port 80 traffic, and wait for DNS resolution to take effect.

Starting an Nginx server requires different steps according to different operating systems: Linux/Unix system: Install the Nginx package (for example, using apt-get or yum). Use systemctl to start an Nginx service (for example, sudo systemctl start nginx). Windows system: Download and install Windows binary files. Start Nginx using the nginx.exe executable (for example, nginx.exe -c conf\nginx.conf). No matter which operating system you use, you can access the server IP
