V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
yazoox
V2EX  ›  NGINX

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

  •  
  •   yazoox · 2020-03-04 13:01:33 +08:00 · 3633 次点击
    这是一个创建于 1485 天前的主题,其中的信息可能已经有所发展或是发生改变。

    楼主搭了一个 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 更好

    第 1 条附言  ·  2020-03-05 16:14:52 +08:00

    Solution:

    1. refer to

    https://stackoverflow.com/questions/23948527/13-permission-denied-while-connecting-to-upstreamnginx

    https://serverfault.com/questions/634294/nodejs-nginx-error-13-permission-denied-while-connecting-to-upstream

    运行下面的两个命令

    yum install policycoreutils-python

    setsebool -P httpd_can_network_connect 1

    1. 直接编辑/etc/nginx/nginx.conf, 注释掉

    include /etc/nginx/conf.d/*.conf;

    ->

    #include /etc/nginx/conf.d/*.conf;

    然后紧接着添加Jackett 服务的设置

    server {
        server_name localhost;
    
        location / {
            proxy_pass         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;
        }
    }
    

    然后,直接打开IP地址或者域名,默认80端口,成功打开Jackett的web UI. 谢谢大家的帮助,给了我很多启发!

    26 条回复    2020-03-05 16:15:33 +08:00
    gwy15
        1
    gwy15  
       2020-03-04 13:07:37 +08:00
    你把 /jackett/ 映射到了 9117,但是你的 jackett 服务器的 base url 还是 /,所以 upstream 会返回 404,然后 fallback 到 nginx 的 404。你需要把 jackett 的 base url 改到 /jackett。
    yazoox
        2
    yazoox  
    OP
       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
        3
    gzlock  
       2020-03-04 13:42:30 +08:00 via Android
    proxy_pass http://127.0.0.1:9117/;
    注意结尾要添加斜杠
    chotow
        4
    chotow  
       2020-03-04 13:48:20 +08:00
    进了 nginx 的默认 server 块吧?而 jackett.conf 里没设置域名,所以不管怎么配置都无效。
    yazoox
        5
    yazoox  
    OP
       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
        6
    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
        7
    blindie  
       2020-03-04 14:11:30 +08:00
    我猜你是没重启 nginx 所以 Nginx 拿到你的 url 就按照默认规则去找了 /usr/share/nginx/html/jackett
    hcymk2
        8
    hcymk2  
       2020-03-04 14:13:40 +08:00
    /jackett/ ?????
    idclight
        9
    idclight  
       2020-03-04 14:17:08 +08:00
    配置文件贴出来。。。我给你改
    idclight
        10
    idclight  
       2020-03-04 14:27:09 +08:00
    我瞎了。。。简单点说。你在 proxy_pass http://locahost:9117 后面加上 / 就好了。就是 proxy_pass http://locahost:9117/
    Niphor
        11
    Niphor  
       2020-03-04 14:56:01 +08:00
    proxy_pass http://127.0.0.1:9117/;

    或楼上
    yazoox
        12
    yazoox  
    OP
       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
        13
    idclight  
       2020-03-04 15:55:12 +08:00
    @yazoox 给个联系方式。。我给你看看
    yazoox
        14
    yazoox  
    OP
       2020-03-04 16:18:46 +08:00
    @idclight
    Wechat:alex-ya
    SakuraKuma
        15
    SakuraKuma  
       2020-03-04 16:53:05 +08:00
    location /jackett/ 改 /
    dier
        16
    dier  
       2020-03-04 20:00:59 +08:00 via iPhone
    添加多个 server
    server 里面通过多个 server_name (不同域名)来区分
    ZField
        17
    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
        18
    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
        19
    also24  
       2020-03-04 20:18:11 +08:00
    楼主你是不是忘了配置 server_name ?
    also24
        20
    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;
    }
    }
    dorothyREN
        21
    dorothyREN  
       2020-03-04 20:28:05 +08:00
    竟然没有 listen 跟 server_name 这俩参数
    wangqianwei
        22
    wangqianwei  
       2020-03-04 21:13:04 +08:00
    源码地址有文档那么复杂,在默认的配置文件加一个 location 就好

    下面的是他的示例:

    Example config for Nginx:

    location /jackett {
    proxy_pass http://127.0.0.1:9117;
    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;
    proxy_set_header X-Forwarded-Host $http_host;
    proxy_redirect off;
    }
    wangqianwei
        23
    wangqianwei  
       2020-03-04 21:14:28 +08:00
    lbyo
        24
    lbyo  
       2020-03-04 21:55:58 +08:00
    40 分钟过去了,楼主搞好没,啥原因
    yanyueio
        25
    yanyueio  
       2020-03-04 22:08:33 +08:00 via Android
    逐一排查一下,location 后面的 /,proxypass 就不要 / 了',最后是否配置了 Servername,或者是否命中了 conf 文件。最后两种可能几率笑,我经历过的大概率的是 /,比如在 sub path 下配置 phpmyadmin,当然那里还有静态资源 rewrite 的问题。如果是其他情况,那基本也逃脱不过 nginx 转发服务的范畴吧,囧。
    yazoox
        26
    yazoox  
    OP
       2020-03-05 16:15:33 +08:00
    搞定!
    有兴趣的可以看我前面的附言!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5448 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 08:45 · PVG 16:45 · LAX 01:45 · JFK 04:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.