简体   繁体   English

如何使用Nginx反向代理将localhost:9292 / json重定向到localhost:80 /?

[英]How to redirect localhost:9292/json to localhost:80/ using Nginx reverse proxy?

 server {   
        listen 80;
        server_name localhost;
        location / {
            index index.html;
            root /Users/Lin/Codes/JS/Emberjs/yeoman-ember/dist;
        }  

        location ~* ^/json {
            root
            proxy_pass http://localhost:9292;

        }
    }

The configure kinda works, but it only pass 配置还可以,但是只能通过

localhost:9292/json to localhost/json . localhost:9292/jsonlocalhost/json

But What I want is 但是我想要的是

localhost:9292/json to 'localhost' localhost:9292/json为'localhost'

localhost:9292/json/post to 'localhost/post' localhost:9292/json/post到“本地主机/ post”

I think what I need to do is set root or do some rewrite, Anyone has some idea? 我认为我需要做的是设置root或进行一些重写,有人有想法吗?

If you want to pass all connections from port 9092 to 80 you are listening the wrong port. 如果要将所有连接从端口9092传递到80,则您正在侦听错误的端口。

Change the port you are listening to 9092: 更改您正在侦听的端口9092:

server {   
    listen 9092;
    server_name localhost;

    root /Users/Lin/Codes/JS/Emberjs/yeoman-ember/dist;

    location / {
        index index.html;

    }  

    location ~* ^/json {
        proxy_pass http://localhost:80;
        proxy_set_header  X-Real-IP  $remote_addr;
    }
}

Try to avoid use root inside the location block, it's a common pitfall as explained in nginx documentation 尝试避免在位置块内使用root,这是一个常见的陷阱,如nginx文档中所述

Also you will need to configure another server to listen port 80. 另外,您将需要配置另一台服务器以侦听端口80。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Nginx反向代理将localhost:9292 / json重写为localhost:80 /? - How to rewrite localhost:9292/json to localhost:80/ using Nginx reverse proxy? 如何使用nginx作为反向代理将localhost:9292定向到子域foo.localhost /? - How to use nginx as reverse proxy to direct localhost:9292 to a sub domain foo.localhost/? 简单地从本地主机:80到本地主机:8080的nginx反向代理不起作用 - Simply nginx reverse proxy from localhost:80 to localhost:8080 not working nginx反向代理到本地主机 - nginx reverse proxy to localhost 如何在本地主机上设置Nginx反向代理 - How to set nginx reverse proxy at localhost nginx反向代理以避免本地主机的cors错误 - nginx reverse proxy to avoid cors error with localhost nginx反向代理到在localhost上运行的后端 - nginx reverse proxy to backend running on localhost Nginx反向代理不适用于本地主机上的Nodejs - Nginx Reverse Proxy not working with Nodejs at localhost Nginx:如何将代理从外部请求反向到特定的 localhost url - Nginx: How to reverse proxy from external request to specific localhost url 如何在 Docker for Windows 上使用 nginx 作为本地主机的反向代理? - How to use nginx on Docker for Windows as reverse proxy for localhost?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM