news 2026/8/26 13:42:15

nginx部署多个项目

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
nginx部署多个项目

前言

实现在一台服务器上使用nginx部署多个项目的方法

查看并修改nginx安装的默认配置文件

在 Linux 操作系统中,Nginx 在编译安装时默认的配置文件路径是/usr/local/nginx/conf/nginx.conf
如果是通过发行版的包管理器安装,则默认的配置文件路径可能会相应改变,例如在 Ubuntu 下为/etc/nginx/nginx.conf

user root; worker_processes auto; worker_rlimit_nofile 65535; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 65535; } http { include mime.types; default_type application/octet-stream; open_file_cache max=102400 inactive=20s; log_format main '[$time_local],$remote_addr,$request_time/$upstream_response_time,$remote_user,"$request",' '$status,$body_bytes_sent,"$http_referer",' '"$http_user_agent","$http_x_forwarded_for"'; access_log logs/access.log main; #access_log on; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; gzip on; gzip_min_length 1000; gzip_buffers 16 8k; gzip_comp_level 9; gzip_proxied any; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml text/javascript application/javascript image/jpeg image/gif image/png application/x-shockwave-flash; #add_header ServerIP 192.169.18.153; client_max_body_size 500m; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; } #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; #} } include conf/*.conf; }

添加、修改或删除 Nginx 的配置,可以编辑该文件,并使用nginx -s reload命令重新加载配置文件。
include conf/*.conf; 配置多个项目conf文件

基于域名配置

基于域名配置,前提是先配置好了域名解析。
域名:www.as.com, 配置了2个二级域名:a.as.com、 b.as.com。

配置 a.as.com 的配置文件:

文件路径:vim /usr/local/nginx/conf/conf/a.conf

server { listen 80; # 端口 需要服务器开放端口 # 域名绑定需要将域名解析A记录到改服务器ip server_name a.as.com; # 你的域名 如果需要ip访问请注释该行并改变端口 location / { root /data/a/dist; index index.html; #解决页面刷新404问题 try_files $uri $uri/ /index.html; } }
配置 b.as.com 的配置文件:

文件路径:vim /usr/local/nginx/conf/conf/b.conf

server { listen 80; server_name b.as.com; location / { root /data/b/dist; index index.html; try_files $uri $uri/ /index.html; } }

配置node项目绑定域名

server { listen 80; server_name api.as.com; location / { proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header Host $http_host; proxy_set_header X-Nginx-Proxy true; proxy_set_header Connection ""; proxy_pass http://0.0.0.0:3000; proxy_read_timeout 18000; # 设置超时 } }

HTTPS(SSL)配置

server { listen 443 ssl; # 端口 server_name api.as.com; # 域名 # 文件目录 /usr/local/nginx/conf/conf/cert ssl_certificate conf/cert/xxx.pem # 证书路径 pem or crt; ssl_certificate_key conf/cert/xxx.key; # 私钥 ssl_session_cache shared:SSL:1m; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; location / { # 这里可以配置静态服务器 or 代理 } } server { listen 80; listen 443 ssl http2; server_name www.as.com; #rewrite ^(.*)$ https://$host$1; #将所有HTTP请求通过rewrite指令重定向到HTTPS。 root /usr/local/app/juroom-admin/; index index.html index.htm; try_files $uri $uri/ /index.html; charset utf-8; access_log logs/www.as.com.access.log main; error_log logs/www.as.com.error.log; ssl_certificate conf/cert/www.as.com.pem; ssl_certificate_key conf/cert/www.as.com.key; ssl_session_timeout 5m; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_prefer_server_ciphers on; client_max_body_size 100m; #上传文件大小限制 location / { root /usr/local/app/juroom-admin/; index index.html index.htm; #解决页面刷新404问题 try_files $uri $uri/ /index.html; if ($request_filename ~* .*\.(?:htm|html)$) { #add_header Cache-Control "private, no-store, no-cache, must-revalidate, proxy-revalidate"; add_header Cache-Control no-cache; } if ($request_filename ~* .*\.(?:js|css)$) { expires 30d; } if ($request_filename ~* .*\.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|svgz|mp4|ogg|ogv|webm)$) { expires 30d; } } }

在 /usr/local/nginx/conf/conf/cert 里放入 .pem 与 .key 文件

nginx.conf常用属性讲解

#定义Nginx运行的用户和用户组 user www www; #nginx进程数,建议设置为等于CPU总核心数。 worker_processes 8; #全局错误日志定义类型,[ debug | info | notice | warn | error | crit ] error_log /usr/local/nginx/logs/error.log info; #进程pid文件 pid /usr/local/nginx/logs/nginx.pid; #设定http服务器,利用它的反向代理功能提供负载均衡支持 http { #常用配置 #设定通过nginx上传文件的大小 client_max_body_size 8m; #长连接超时时间,单位是秒 keepalive_timeout 120; #默认文件类型 default_type application/octet-stream; #默认编码 #charset utf-8; #负载均衡配置 upstream as.test { #upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。 server 192.168.80.121:80 weight=3; server 192.168.80.122:80 weight=2; server 192.168.80.123:80 weight=3; #nginx的upstream目前支持4种方式的分配 #1、轮询(默认) #每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。 #2、weight #指定轮询几率,weight和访问比率成正比,用于后端服务器性能不均的情况。 #例如: #upstream bakend { # server 192.168.0.14 weight=10; # server 192.168.0.15 weight=10; #} #3、ip_hash #每个请求按访问ip的hash结果分配,这样每个访客固定访问一个后端服务器,可以解决session的问题。 #例如: #upstream bakend { # ip_hash; # server 192.168.0.14:88; # server 192.168.0.15:80; #} } #虚拟主机的配置 server { #监听端口 listen 80; #域名可以有多个,用空格隔开 server_name www.w3cschool.cn w3cschool.cn; index index.html index.htm index.php; root /data/www/w3cschool; #定义本虚拟主机的访问日志 access_log /usr/local/nginx/logs/host.access.log main; access_log /usr/local/nginx/logs/host.access.404.log log404; #对 "/test" 启用反向代理 location /test { # 所有请求了 ip:port/test/** 的接口 都会转发到 as.test 负载配置去 proxy_pass as.test; # 或者直接配置地址 # proxy_pass http://127.0.0.1:88; #允许客户端请求的最大单文件字节数 client_max_body_size 10m; #后端服务器连接的超时时间_发起握手等候响应超时时间 #nginx跟后端服务器连接超时时间(代理连接超时) proxy_connect_timeout 90; #后端服务器数据回传时间(代理发送超时) #后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据 proxy_send_timeout 90; #连接成功后,后端服务器响应时间(代理接收超时) #连接成功后_等候后端服务器响应时间_其实已经进入后端的排队之中等候处理(也可以说是后端服务器处理请求的时间) proxy_read_timeout 90; } } }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/26 13:39:36

Vue3瀑布流(Waterfall)

Vue2瀑布流(Waterfall) 瀑布流_百度百科 可自定义设置以下属性: 图片数组(images),类型:Image[],默认 [] 要划分的列数(columnCount),类型&am…

作者头像 李华
网站建设 2026/8/26 13:35:53

OpenAI王冠松动:多模型时代开发者选型与API接入实战

最近这半年,AI 圈最明显的感受不是某个模型又刷了多少分,而是 OpenAI 不再拥有绝对的“王冠”式统治力。Claude 在编程场景里强势反超,Gemini 在长上下文和原生多模态上持续发力,开源模型也在快速逼近闭源第一梯队。标题里说 Open…

作者头像 李华
网站建设 2026/8/26 13:35:26

如何使用界面控件DevExpress WinForms自带的UI模板?其实很简单

DevExpress WinForms拥有180组件和UI库,能为Windows Forms平台创建具有影响力的业务解决方案。DevExpress WinForms能完美构建流畅、美观且易于使用的应用程序,无论是Office风格的界面,还是分析处理大批量的业务数据,它都能轻松胜…

作者头像 李华
网站建设 2026/8/26 13:34:07

云Agent选型与部署实践:从控制面模型到运维自动化

如果你经常逛 Hacker News,大概率见过一种很典型的提问方式:Ask HN: What cloud agents do you use? 下面密密麻麻的回复里,有人推荐某云厂商的原生 Agent,有人坚持开源方案,也有人直接说“别用 Agent,SSH…

作者头像 李华
网站建设 2026/8/26 13:31:30

Python+Demucs实战:仅凭贝斯声轨识别BEYOND歌曲

之前在整理音频素材时,我冒出一个有点挑战性的想法:把BEYOND的经典歌曲做一次“去人声、去吉他、去鼓”,只保留贝斯轨,然后不看任何提示,只听贝斯声音猜歌。结果发现,这个玩法比想象中难,也比想…

作者头像 李华
网站建设 2026/8/26 13:30:42

加密算法笔记(哈希算法、base64,md5,aes,rsa等)、签名校验、bcrypt、数字信封

开发中经常要用到加解密,熟悉它很有好处。 文章目录哈希算法(hash)(消息摘要算法)单向散列哈希算法哈希算法可逆吗?哈希算法的时间复杂度哈希算法是加密算法吗?md5DigestUtils(spring)实现md5DigestUtils验证密码(登录、修改密码)DigestUtils新增用户时加密对称加…

作者头像 李华