2024-05-07 21:55阅读: 87评论: 0推荐: 0

Nginx代理设置

Nginx代理设置

需求

只有一个服务器,但是有多个应用在不同端口,需要通过域名或ip加路径的组合实现访问,不能是ip:port这样来访问

工具

Nginx

copy
  • 1
sudo apt install nginx

Nginx配置

先上配置,配置文件位置:/etc/nginx/nginx.conf

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
# 找到http部分 http { ..........省略部分默认配置 include /etc/nginx/mime.types; default_type application/octet-stream; # 下面是自己加的部分 client_max_body_size 1g; #最大接受1g文件以内的 server { listen 80; server_name localhost; # 代表这个server直接用ip访问 location /foo { proxy_pass http://localhost:8080/; } location /bar/ { proxy_pass http://localhost:8888/; } } server { listen 80; server_name example.com; # 代表用域名访问 location / { proxy_pass http://localhost:8080/; } } server { listen 80; server_name foo.example.com; # 用子域名来区分服务 location / { proxy_pass http://localhost:8888/; } } include /path/to/server/conf; # 导入外部的server配置,避免全部写一堆 ## # SSL Settings ## ..........省略部分默认配置 #! 下面的玩意得注释了,不然直接用ip访问不行 #include /etc/nginx/conf.d/*.conf; #include /etc/nginx/sites-enabled/*; }

如上:

基本格式是

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
http { server { listen 80; # 监听的端口 server_name localhost; # 请求的域名或者ip,对其进行匹配解析 location / { proxy_pass http://localhost:port/; # 将其转发到具体的ip和端口 } } }

一个server对应一个server_name,也就是域名

location这是匹配具体的路由,对不同路由,用proxy_pass进行转发,到实际的服务地址

注意http://localhost:port/后面的/,不加那么实际请求会保留匹配的路由,加了就以匹配的路由为根

对于location /foo { proxy_pass http://localhost:8080; }

当访问 http://example.com/foo时,转发后的实际请求是 http://localhost:8080/foo

若是加了/,则转发后的实际请求是http://localhost:8080/

这样,通过不同的域名和路由,只访问80端口而实现了对不同端口的服务的访问

Nginx常用命令

copy
  • 1
  • 2
  • 3
  • 4
nginx -s reload # 重新加载配置,每次修改后使用 nginx -t # 检验修改的配置是否正确 nginx -c conf_path # 选择配置文件,若发现reload有问题,可以用这个重新加载一下配置文件 nginx -t -c conf_path # 选择配置文件并检验

跨域问题

跨域的定义
同源策略限制了从同一个源加载的文档或脚本如何与来自另一个源的资源进行交互。这是一个用于隔离潜在恶意文件的重要安全机制。通常不允许不同源间的读操作。

同源的定义
如果两个页面的协议,端口(如果有指定)和域名都相同,则两个页面具有相同的源。

nginx解决跨域的原理
例如:

前端server域名为:http://xx_domain
后端server域名为:https://github.com
现在http://xx_domain对https://github.com发起请求一定会出现跨域。

不过只需要启动一个nginx服务器,将server_name设置为xx_domain,然后设置相应的location以拦截前端需要跨域的请求,最后将请求代理回github.com。如下面的配置:

配置反向代理的参数

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
server { listen 8080; server_name xx_domain ## 1. 用户访问 http://xx_domain,则反向代理到 https://github.com location / { proxy_pass https://github.com; proxy_redirect off; proxy_set_header Host $host; # 传递域名 proxy_set_header X-Real-IP $remote_addr; # 传递ip proxy_set_header X-Scheme $scheme; # 传递协议 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

这样可以完美绕过浏览器的同源策略:github.com访问nginx的github.com属于同源访问,而nginx对服务端转发的请求不会触发浏览器的同源策略。

启用SSL

首先要申请ssl证书并下载证书和私钥文件到服务器,具体参见Nginx或Tengine服务器配置SSL证书_数字证书管理服务(原SSL证书)(SSL Certificate)-阿里云帮助中心

copy
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
server { listen 443 ssl; # ssl代表启用了ssl server_name xx_domain; ssl_certificate /ssl/cert.pem; # 这里要填写证书文件绝对路径 ssl_certificate_key /ssl/cert.key; # 填写证书私钥文件绝对路径 # 只有上面是多的部分,下面和http一样 location / { proxy_pass http://localhost:8080/; proxy_redirect off; proxy_set_header Host $host; # 传递域名 proxy_set_header X-Real-IP $remote_addr; # 传递ip proxy_set_header X-Scheme $scheme; # 传递协议 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }

参考

如何用一台服务器,部署多个不同的 WEB 项目 (use nginx in docker) - 知乎 (zhihu.com)

如果遇到一些问题,可以参考nginx反向代理配置一个域名映射到不同端口的项目,nginx常见问题整理_nginx一个域名转发多个端口-CSDN博客

Nginx大文件的上传下载与优化 - 知乎 (zhihu.com)

比较经典的问题就是文件请求大小设置,在http下面添加client_max_body_size 3m; #最大接受3m文件以内的保证能发送大文件,比如我是网盘服务,就设的1g,不然文件大点就传输中断

这个参数默认是1m设置成0就没有限制了。

我遇到的另外一个问题是下载文件系统就崩了,看了下监考实例云盘读写BPS飙得很高,好像是sendfile有优化,导致系统反而崩了,所以我就把它关了,之后没有深入测试,但是大体好像没问题了。

nginx详细参数配置(史上最全) - hanease - 博客园 (cnblogs.com)

本文作者:faf4r

本文链接:https://www.cnblogs.com/faf4r/p/18178498

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   faf4r  阅读(87)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
💬
评论
📌
收藏
💗
关注
👍
推荐
🚀
回顶
收起