【问题标题】:How do I redirect all requests to an application running on a specific port using nginx?如何使用 nginx 将所有请求重定向到在特定端口上运行的应用程序?
【发布时间】:2016-07-26 19:06:38
【问题描述】:

我正在端口 8000 上运行应用程序,并且需要将端口 80 上的所有请求重定向到我的应用程序(端口 8000)。我知道使用 nginx 是解决问题的方法。我修改了 ngninx.conf 如下:

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;

            root /usr/share/nginx/html;
            index index.html index.htm;

            server_name localhost;

            location / {
                    proxy_pass http://127.0.0.1:8000;
                    try_files $uri $uri/ =404;
            } 
}

我现在可以将请求重定向到我的应用程序服务器,但是 xyz.com/abc 形式的请求返回 404 未找到。我以为上面会匹配所有请求并将所有内容重定向到端口 8000 上的应用程序。我在这里做错了什么?

【问题讨论】:

    标签: redirect nginx


    【解决方案1】:

    try_files 根据服务器块中定义的root 检查文件是否存在。它在location / 块中所做的是检查文件,然后在找不到时返回404。也许您希望您的配置看起来像这样?:

    server {
            listen 80 default_server;
            listen [::]:80 default_server ipv6only=on;
    
            root /usr/share/nginx/html;
            index index.html index.htm;
    
            server_name localhost;
    
            location / {
                try_files $uri $uri/ @my_proxy;
            }
    
            location @my_proxy {
                    proxy_pass http://127.0.0.1:8000;
            } 
    }
    

    这里,如果try_files 找不到任何东西,它会将请求传递给运行在端口8000 上的应用服务器。如果它仍然返回404,则该文件确实不存在。

    【讨论】:

      猜你喜欢
      • 2018-11-10
      • 2015-09-30
      • 1970-01-01
      • 2017-05-04
      • 1970-01-01
      • 1970-01-01
      • 2022-08-19
      • 2020-05-12
      • 2020-09-03
      相关资源
      最近更新 更多