springboot+VUE+部署(11。Nginx)

  • CentOS/RHEL: 执行 sudo yum install nginx(8+ 系统用 dnf install nginx)
  • Ubuntu/Debian: 执行 sudo apt update && sudo apt install nginx

然后等待安装完成。

安装完成后,可以使用以下命令查看Nginx服务:

sudo systemctl status ngin

复制代码
root@ser988850221666:/etc/caddy# sudo systemctl status nginx
● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Sun 2026-02-15 12:54:46 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 1883021 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 1883022 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 1883023 (nginx)
      Tasks: 2 (limit: 1099)
     Memory: 12.0M
        CPU: 114ms
     CGroup: /system.slice/nginx.service
             ├─1883023 "nginx: master process /usr/sbin/nginx -g daemon on; master_process on;"
             └─1883024 "nginx: worker process" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Feb 15 12:54:46 ser988850221666 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 15 12:54:46 ser988850221666 systemd[1]: Started A high performance web server and a reverse proxy server.

在浏览器中输入服务器IP或域名,若看到"Welcome to nginx!"页面,说明安装成功。

启动并设为开机自启

Nginx 安装后默认不自动运行,需手动启用。

  • 启动服务:sudo systemctl start nginx
  • 设为开机自启:sudo systemctl enable nginx
  • 检查监听状态:sudo ss -tlnp | grep :80(应看到 nginx 占用 80 端口
  • 打开防火墙(如启用):sudo firewall-cmd --permanent --add-service=http(CentOS)或 sudo ufw allow 'Nginx Full'(Ubuntu)

开放HTTP(80)和HTTPS(443)端口:

  1. sudo ufw allow 80/tcp

  2. sudo ufw allow 443/tcp

  3. sudo ufw reload

对于firewalld(RHEL/CentOS):

  1. sudo firewall-cmd --permanent --add-service=http

  2. sudo firewall-cmd --permanent --add-service=https

  3. sudo firewall-cmd --reload

卸载Nginx

如需卸载,删除安装目录并清理服务文件:

  1. sudo rm -rf /usr/local/nginx

  2. sudo rm /etc/systemd/system/nginx.service

  3. sudo systemctl daemon-reload

上传前端文件

将前端项目打包后的文件(如distbuild目录)上传到服务器,通常放在/var/www/html目录下:

配置Nginx

编辑Nginx配置文件,通常位于/etc/nginx/sites-available/default

sudo vi /etc/nginx/sites-available/default

复制代码
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name _;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;
	}

	# pass PHP scripts to FastCGI server
	#
	#location ~ \.php$ {
	#	include snippets/fastcgi-php.conf;
	#
	#	# With php-fpm (or other unix sockets):
	#	fastcgi_pass unix:/run/php/php7.4-fpm.sock;
	#	# With php-cgi (or other tcp sockets):
	#	fastcgi_pass 127.0.0.1:9000;
	#}

	# deny access to .htaccess files, if Apache's document root
	# concurs with nginx's one
	#
	#location ~ /\.ht {
	#	deny all;
	#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
server {
	listen 80;
	listen [::]:80;

	server_name 1.1.1.1;#你的IP或域名

	root /var/www/html/dist;
	index index.html;

	location / {
		try_files $uri $uri/ =404;
	}
}

验证Nginx配置语法

sudo nginx -t

复制代码
root@ser988850221666:/etc/nginx/sites-available# vi default
root@ser988850221666:/etc/nginx/sites-available# sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

重载Nginx配置

  1. sudo systemctl reload nginx
相关推荐
云边有个稻草人2 小时前
【Linux系统】进程地址空间
linux·虚拟地址空间·进程地址空间·虚拟地址空间是怎么实现的?·为什么要有虚拟地址空间?·怎么理解虚拟地址空间?
xiangxiongfly9153 小时前
Vue3 根据角色权限动态加载路由
前端·javascript·vue.js·动态加载路由
Aolith4 小时前
我是怎么把个人论坛首页性能从80分优化到100分的(附踩坑全记录)
vue.js·性能优化
Amy_yang4 小时前
uni-app 安卓端纯前端预览 DOCX 的实现思路
前端·vue.js
xiangxiongfly9155 小时前
Vue3 动态加载静态资源
前端·javascript·vue.js
克里斯蒂亚诺更新5 小时前
ruoyi切换新版本初始化需要修改的地方
前端·javascript·vue.js
前端那点事5 小时前
Vite+Vue3环境判断终极解法!区分开发/生产环境,告别环境报错
前端·vue.js
程序员老邢6 小时前
【技术底稿 32】Nginx 经典大坑复盘:本机公网域名自环代理,导致接口返回首页 / 404 实战排障
java·运维·nginx·前后端分离·技术底稿·后端部署
ZHIS6 小时前
移动端 Vue3 高清 PDF 预览组件开发:支持手势缩放 + 按钮缩放 + 加载进度
vue.js
Amy_yang6 小时前
uni-app 中 web-view 的使用与 App 端全屏问题处理
前端·javascript·vue.js