首页 > 解决方案 > windows上的nginx不重定向?

问题描述

我正在我的 Windows 10 机器上本地使用 minikube 开发 Kubernetes 部署服务,所以当我公开我的 expressjs API 服务时,我可以通过以下方式访问它:localhost:3000

在此处输入图像描述

在此处输入图像描述

我想在网络上公开该服务,以便可以从另一台设备(我的手机)测试localhost:3000/api API Nginx 的默认页面?

在此处输入图像描述

这是我的 nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;
        location / {
                proxy_pass http://localhost:3000/api/;
                }

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

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #    root           html;
        #    fastcgi_pass   127.0.0.1:9000;
        #    fastcgi_index  index.php;
        #    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        #    include        fastcgi_params;
        #}

        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server
    #
    #server {
    #    listen       443 ssl;
    #    server_name  localhost;

    #    ssl_certificate      cert.pem;
    #    ssl_certificate_key  cert.key;

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

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

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}

}
`

标签: nginxkubernetesnginx-reverse-proxyminikubenginx-config

解决方案


这里可能会发生一些事情:

  1. Nginx 配置更改后,没有执行应用的停止和重新加载;这是加载新配置所必需的,并使用命令执行nginx -s stop,然后nginx -s reload

  2. 意外运行多个实例:如果由于某种原因该命令start nginx多次运行,则每次都会运行一个进程,并且无法用该nginx -s stop命令杀死它们;在这种情况下,您将需要终止任务管理器上的进程或重新启动 Windows 系统。

  3. 请注意,Nginx for Windows 被视为beta版本,它存在一些性能和可操作性问题,如以下文档 [1] 中所述。

我建议切换到完全支持 Minikube 和 Nginx 的 Linux 系统。您的方案已在 Ubuntu VM 上复制,并且按预期工作。

[1] http://nginx.org/en/docs/windows.html


推荐阅读