让Nginx帮助本地开发中的重定向

让Nginx帮助本地开发中的重定向,nginx,Nginx,我有一个内置的网站。在我的Mac电脑中,我可以使用warn和PORT=8000 HTTPS=true./node_modules/.bin/react scripts start在http://localhost:8000 现在,我想将Nginx添加到我的Mac中,以模拟生产环境,尤其是重定向环境。我刚刚用brew在mac上安装了Nginx,下面是/usr/local/etc/Nginx/Nginx.conf: worker_processes 1; events { worker_

我有一个内置的网站。在我的Mac电脑中,我可以使用
warn
PORT=8000 HTTPS=true./node_modules/.bin/react scripts start
http://localhost:8000

现在,我想将Nginx添加到我的Mac中,以模拟生产环境,尤其是重定向环境。我刚刚用brew在mac上安装了Nginx,下面是
/usr/local/etc/Nginx/Nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

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

    sendfile        on;

    keepalive_timeout  65;

    server {
        listen       8080;
        server_name  localhost;

        location / {
            proxy_set_header    Host                $host;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto   $scheme;
            proxy_set_header    Accept-Encoding     "";
            proxy_set_header    Proxy               "";
            proxy_pass          http://localhost:8000/;

            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
        }

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

    server {
       listen       443 ssl;
       server_name  localhost;

       ssl_certificate      /etc/ssl/localhost/localhost.crt;
       ssl_certificate_key  /etc/ssl/localhost/localhost.key;

       ssl_session_cache    shared:SSL:1m;
       ssl_session_timeout  5m;

       ssl_ciphers  HIGH:!aNULL:!MD5;
       ssl_prefer_server_ciphers  on;

       location ~ /google {                                                                                            
           proxy_pass  https://www.google.com;
       }

       location ~ /faceboook {                                                                                            
           rewrite ^ 'https://www.facebook.com' redirect;
       }

       location / {
            proxy_set_header    Host                $host;
            proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
            proxy_set_header    X-Forwarded-Proto   $scheme;
            proxy_set_header    Accept-Encoding     "";
            proxy_set_header    Proxy               "";
            proxy_pass          https://localhost:8000/;

            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection "upgrade";
       }
    }
    include servers/*;
}
结果是,
https://localhost:443
在浏览器中重定向到
https://localhost/#/
并运行我的网站

但是,

  • 我总是希望域是
    https://localhost:8000
    而不是
    https://localhost:443
    https://localhost/
  • 另外,我想要
    https://localhost:8000/google
    重定向到
    https://www.google.com
    https://localhost:8000/facebook
    重定向到
    https://www.facebook.com
    因为Nginx

  • 有人知道如何实现这一点吗?

    首先,我将在端口8080上运行应用程序,然后执行第2步

    server {
            listen 8000 ssl;
            ssl_certificate      /etc/ssl/localhost/localhost.crt;
            ssl_certificate_key  /etc/ssl/localhost/localhost.key;
            location /{
                proxy_pass http://localhost:8080; # app runs on porn 8080
                # if backend has ssl enabled
                #proxy_pass https://localhost:8080;
                #proxy_ssl_verify              off;
            }
    
            location /facebook {
                      rewrite ^(/.*)$ https://www.facebook.com permanent;
            }
    
            location /google {
                      rewrite ^(/.*)$ https://www.google.com permanent;
            }
    }
    

    首先,您必须小心使用
    代理通行证上的
    /
    尾随斜杠
    ):

    配置应如下所示:

    worker_processes  1;
    
    events {
        worker_connections  1024;
    }
    
    http {
        include       mime.types;
        default_type  application/octet-stream;
    
        sendfile        on;
    
        keepalive_timeout  65;
    
        server {
            listen 8000 ssl;
            server_name localhost;
            ssl_certificate      /etc/ssl/localhost/localhost.crt;
            ssl_certificate_key  /etc/ssl/localhost/localhost.key;
            
            root <PATH_TO_APPS>;
            index index.html;
            
            location / {
                try_files $uri $uri/ /index.html;
            }
    
            location /facebook {
                rewrite ^(/.*)$ https://www.facebook.com permanent;
            }
    
            location /google {
                rewrite ^(/.*)$ https://www.google.com permanent;
            }
        }
    
        server {
           listen       443 ssl;
           server_name  localhost;
    
           ssl_certificate      /etc/ssl/localhost/localhost.crt;
           ssl_certificate_key  /etc/ssl/localhost/localhost.key;
    
           ssl_session_cache    shared:SSL:1m;
           ssl_session_timeout  5m;
    
           ssl_ciphers  HIGH:!aNULL:!MD5;
           ssl_prefer_server_ciphers  on;
    
           location / {
                return 301 https://localhost:8000;
           }
        }
    
        include servers/*;
    }
    
    worker\u进程1;
    事件{
    工人(1024);;
    }
    http{
    包括mime.types;
    默认_类型应用程序/八位字节流;
    发送文件到;
    保持生命超时65;
    服务器{
    倾听;
    服务器名称localhost;
    ssl\U证书/etc/ssl/localhost/localhost.crt;
    ssl\u certificate\u key/etc/ssl/localhost/localhost.key;
    根;
    index.html;
    地点/{
    尝试_文件$uri$uri//index.html;
    }
    地点/facebook{
    重写^(/.*)$https://www.facebook.com 永久的;
    }
    位置/谷歌{
    重写^(/.*)$https://www.google.com 永久的;
    }
    }
    服务器{
    听443ssl;
    服务器名称localhost;
    ssl\U证书/etc/ssl/localhost/localhost.crt;
    ssl\u certificate\u key/etc/ssl/localhost/localhost.key;
    ssl_会话_缓存共享:ssl:1m;
    ssl_会话_超时;
    ssl_密码高:!aNULL:!MD5;
    ssl首选服务器上的密码;
    地点/{
    返回301https://localhost:8000;
    }
    }
    包括服务器/*;
    }