真小白,请教一个 nginx 入门的配置问题,有关反向代理的

2020-03-04 13:01:33 +08:00
 yazoox

楼主搭了一个 jackett,想试试这玩意。但是它的端口是 9117,想换成 80 端口。所以,需要 nginx 的反向代理。

参考了一下 Jackett 的官方文档( https://github.com/Jackett/Jackett/wiki/Reverse-Proxy

CentOS7

Jackett 安装在 /home/myname/Jackett 目录下面,端口 9117

e.g. http://myjack.ml:9117 (大家能够看到 Jackett 的登录界面)

nginx 已经安装好了。能够正常启动工作。

e.g. http://myjack.ml (大家能够看到 nginx 的 welcome )

"sudo tail -100 /var/log/nginx/error.log"能够看到下面这句:

2020/03/04 03:49:41 [error] 83094#83094: *2 open() "/usr/share/nginx/html/Jackett" failed (2: No such file or directory), client: xxx.23x.25x.1xx, server: localhost, request: "GET /Jackett HTTP/1.1", host: "myjack.ml"

楼主分析,重定向到 /usr/share/nginx/html/Jackett 这个目录下去了,但楼主改了 指向 jackett 安装目录 root /home/myname/Jackett; 好像没有用。

/etc/nginx/sites-available/jackett.conf

/etc/nginx/sites-enabled/jackett.conf -> ln 到上面这个文件

nginx, /etc/nginx/nginx.conf 里面,我添加了这一句:

include /etc/nginx/sites-enabled/*;

jackett.conf 的内容如下:

server {
    root /home/myname/Jackett; #不论有没有这一句,都会出现上面 error.log 里面的错误

    location /jackett/ {
        proxy_pass         http://127.0.0.1:9117; #http://localhost:9117 错误一样
        proxy_http_version 1.1;
        proxy_set_header   Upgrade $http_upgrade;
        proxy_set_header   Connection keep-alive;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header   X-Forwarded-Proto $scheme;
        proxy_set_header   X-Forwarded-Host $http_host;
    }
}

楼主不会 nginx,只是照葫芦画了一下。有没有高手,帮忙看一眼,这个该如何配置?

万分感谢!

p.s. 我希望能够 http://myjack.ml/jackett 能够正常打开 jackett 的 web UI 当然,最终,我可能会再改改,http://myjack.ml 直接能打开 jackett web UI 更好

3680 次点击
所在节点    NGINX
26 条回复
gwy15
2020-03-04 13:07:37 +08:00
你把 /jackett/ 映射到了 9117,但是你的 jackett 服务器的 base url 还是 /,所以 upstream 会返回 404,然后 fallback 到 nginx 的 404。你需要把 jackett 的 base url 改到 /jackett。
yazoox
2020-03-04 13:35:43 +08:00
@gwy15 thx
不过,我不是很懂你说的 base url 是什么
e.g. http://myjack.ml:9117/UI/Dashboard
你可以看到,Jackett Configuration 里面
Base Path override: /jackett
这是我能够找到的,接近 base url 的地方了。
gzlock
2020-03-04 13:42:30 +08:00
proxy_pass http://127.0.0.1:9117/;
注意结尾要添加斜杠
chotow
2020-03-04 13:48:20 +08:00
进了 nginx 的默认 server 块吧?而 jackett.conf 里没设置域名,所以不管怎么配置都无效。
yazoox
2020-03-04 14:01:28 +08:00
@chotow 兄弟,能具体说说么?我不是很懂 nginx
```
2020/03/04 03:49:41 [error] 83094#83094: *2 open() "/usr/share/nginx/html/Jackett" failed (2: No such file or directory), client: xxx.23x.25x.1xx, server: localhost, request: "GET /Jackett HTTP/1.1", host: "myjack.ml"
```
从 log 看,nginx 应该是找到 jackett 的 server 了吧?


@gzlock 刚才按你说的,添加了 / ,但是好像“涛声依旧”
blindie
2020-03-04 14:09:17 +08:00
proxy_pass 跟 root 没关系的。讲道理你这简单转发在默认配置 80 那块下面直接写一个 location /jacket {proxy_pass http://127.0.0.1:9117}然后重启一下 nginx 服务就可以了。
blindie
2020-03-04 14:11:30 +08:00
我猜你是没重启 nginx 所以 Nginx 拿到你的 url 就按照默认规则去找了 /usr/share/nginx/html/jackett
hcymk2
2020-03-04 14:13:40 +08:00
/jackett/ ?????
idclight
2020-03-04 14:17:08 +08:00
配置文件贴出来。。。我给你改
idclight
2020-03-04 14:27:09 +08:00
我瞎了。。。简单点说。你在 proxy_pass http://locahost:9117 后面加上 / 就好了。就是 proxy_pass http://locahost:9117/
Niphor
2020-03-04 14:56:01 +08:00
proxy_pass http://127.0.0.1:9117/;

或楼上
yazoox
2020-03-04 15:33:32 +08:00
@idclight 不行,我试过了。
/etc/nginx/sites-available/jackett.conf
```
server {
location /jackett/ {
proxy_pass http://127.0.0.1:9117/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
}
}
```

/etc/nginx/nginx.conf
```

user nginx;
worker_processes 1;

error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;


events {
worker_connections 1024;
}


http {
include /etc/nginx/mime.types;
default_type application/octet-stream;

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;

keepalive_timeout 65;

#gzip on;

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
```

/etc/nginx/conf.d/default.conf
```
server {
listen 80;
server_name localhost;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}
```

@blindie 兄弟,你的建议我试过了,简单写法也不行......
idclight
2020-03-04 15:55:12 +08:00
@yazoox 给个联系方式。。我给你看看
yazoox
2020-03-04 16:18:46 +08:00
@idclight
Wechat:alex-ya
SakuraKuma
2020-03-04 16:53:05 +08:00
location /jackett/ 改 /
dier
2020-03-04 20:00:59 +08:00
添加多个 server
server 里面通过多个 server_name (不同域名)来区分
ZField
2020-03-04 20:10:43 +08:00
/etc/nginx/sites-available/jackett.conf
/etc/nginx/nginx.conf
/etc/nginx/conf.d/default.conf

include /etc/nginx/mime.types;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

是不是路径的问题啊
chotow
2020-03-04 20:11:46 +08:00
@yazoox #5 以下是我个人看法,没真实做过实验,仅供参考。

当前实际命中的是 default.conf 这个配置文件,所以日志显示了 /usr/share/nginx/html/Jackett。
而你的目标是命中 jackett.conf ;为什么没命中呢?因为这两个配置文件都没有设置 server_name。
解决办法,删掉 default.conf,或者在 jackett.conf 中配置 server_name。
此外,proxy_pass 后不要跟斜杠,如果跟了斜杠,需要修改 Jackett 的 Base URL 为「/」(当前是「/jackett 」)。
also24
2020-03-04 20:18:11 +08:00
楼主你是不是忘了配置 server_name ?
also24
2020-03-04 20:20:49 +08:00
server {
server_name myjack.ml;

location / {
proxy_pass http://127.0.0.1:9117
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection keep-alive;
proxy_cache_bypass $http_upgrade;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $http_host;
}
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/649741

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX