obsidian/笔记文件/2.笔记/nginx端口转发.md

281 lines
6.9 KiB
Markdown
Raw Permalink Normal View History

2025-04-29 18:28:42 +08:00
#杂七杂八常识
2025-05-09 10:43:12 +08:00
## 安装nginx
如果要安装nginx不同发行版会有细微区别
1、Debian或Ubuntu
``` bash
# 更新软件包索引
sudo apt update
# 安装 Nginx
sudo apt install nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
2、RHEL / CentOS / Rocky Linux / AlmaLinux基于 yum 或 dnf
CentOS 7 / RHEL 7使用 yum
``` bash
# 安装 EPEL 仓库(如果需要)
sudo yum install epel-release
# 安装 Nginx
sudo yum install nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
3、Fedora基于 dnf
``` bash
# 安装 Nginx
sudo dnf install nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
4、Arch Linux / Manjaro基于 pacman
``` bash
# 安装 Nginx
sudo pacman -S nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
5、openSUSE基于 zypper
``` bash
# 安装 Nginx
sudo zypper install nginx
# 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
6、Alpine Linux基于 apk
``` bash
# 安装 Nginx
sudo apk add nginx
# 启动并设置开机自启
sudo rc-service nginx start
sudo rc-update add nginx
```
通过这个指令可以判断nginx是否安装成功
``` bash
systemctl status nginx
```
![[Pasted image 20250509101105.png]]
![[Pasted image 20250509101119.png]]
2025-05-28 11:37:44 +08:00
查看nginx版本同时测试配置文件是否正常可以跑通相关指令
2025-05-09 10:43:12 +08:00
``` bash
nginx -t
```
2025-05-28 11:37:44 +08:00
运行结果:
![[Pasted image 20250528094006.png]]
2025-05-09 10:43:12 +08:00
然后就可以开始修改nginx的相关配置了跳转到对应目录然后`nano`看一下,对应的配置文件
``` bash
cd /etc/nginx/
nano nginx.conf
```
2025-07-30 18:45:29 +08:00
配合[[rz 和 sz linux的上传和下载]]工具,可以把配置文件`nginx.conf`拷贝到,电脑本地;
2025-05-09 10:43:12 +08:00
然后,开始修改配置:同时存在多个,端口转发配置,都是以`server`包裹起来的,结构体,举例说明:
![[Pasted image 20250509102330.png]]
## 完整配置参考:
``` bash
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 4096;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 81;
server_name _;
location / {
root /usr/local/games/errorwebview/dist;
index index.html index.htm;
try_files $uri $uri/ /index.html;
}
location /api/ {
2025-05-27 19:09:21 +08:00
proxy_pass http://127.0.0.1:8080/api/;
2025-05-09 10:43:12 +08:00
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
error_page 404 /404.html;
location = /404.html {
internal;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
internal;
}
}
# 新增的 server 块,监听 82 端口并路由到 3000 端口
server {
listen 82;
server_name _;
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
# 主服务配置80端口
server {
listen 80;
server_name _;
# 处理/report到8080端口
location = /report {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 处理/collect到83端口
location = /collect {
proxy_pass http://127.0.0.1:83;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# 其他请求处理规则(可选)
location / {
# 默认处理规则例如返回404或转发到其他服务
return 404;
}
# 错误页面配置
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
2025-05-27 19:09:21 +08:00
2025-05-09 10:43:12 +08:00
```
需要注意的是80是默认端口这里是把俩服务分别转发到对应的端口例如8080和83
![[Pasted image 20250509102657.png]]
2025-07-30 18:45:29 +08:00
修改完成后,也再通过[[rz 和 sz linux的上传和下载]]把配置文件提交到linux服务器即可如果注意的是如果同文件夹已有同名文件是需要先把同名文件改掉的不然可能会出现提交失败的情况直接使用`mv`修改备份即可
2025-05-09 10:43:12 +08:00
``` bash
mv nginx.conf nginx.conf.bak
```
然后重启一下nginx
``` bash
systemctl reload nginx
```
需要注意的是原本80端口的服务就需要改成8080了不然会出现异常因为nginx端口转发成功了
2025-05-27 19:09:21 +08:00
![[Pasted image 20250509103138.png]]