【问题标题】:nginx config for static and nodejs apps静态和 nodejs 应用程序的 nginx 配置
【发布时间】:2017-01-17 00:21:42
【问题描述】:

我们有多个 nodejs 应用程序在不同的端口上运行并使用 nginx 作为代理。由于nginx.conf 中的一些错误正则表达式,我们在访问静态文件 url 时面临 (504) 问题

任何人都遇到过类似的网址格式。任何指针都会有所帮助

nginx version 1.8.0

504 网关问题

https://localhost:9443/js/app1/index.js
https://localhost:9443/css/app1/index.css

https://localhost:9443/js/app2/index.js
https://localhost:9443/css/app2/index.css

应用网址

https://localhost:9443/app1
https://localhost:9443/app2
https://localhost:9443/api/app1
https://localhost:9443/api/app2

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       9443;
        ssl on;
        ssl_certificate /path/to/ssl_certificate;        # path to your cacert.pem
        ssl_certificate_key /path/to/ssl_certifiatekey;    # path to your privkey.pem
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location /js {
          alias /path/to/static/files;
        }
        location /css {
          alias /path/to/static/files;
        }

        location / {
            proxy_pass https://localhost:8443; #nodejsapp1
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $http_host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_buffering         on;
        }


        location ~ /app1/ {
          proxy_pass https://localhost:8143; #nodejsapp2
          error_page 502 = @fallback;
        }

        location ~ /app2 {
          proxy_pass https://localhost:8343; #nodejsapp3
          error_page 502 = @fallback;
        }

        location @fallback{
            rewrite ^ /maintenance;
            proxy_pass https://localhost:8443;
        }


        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }


    }



    include servers/*;
}

【问题讨论】:

    标签: node.js nginx nginx-location


    【解决方案1】:

    显然https://localhost:9443/js/app1/index.js 匹配app1 的正则表达式,因为它包含文本/app1/

    正则表达式位置优先于普通前缀位置,因此在上述情况下不使用location /js 块。

    阅读the documentation 以了解location 指令的评估顺序。

    您可以使用^~ 修饰符将jscss 位置的优先顺序移到所有正则表达式位置之上:

    location ^~ /js { ... }
    location ^~ /css { ... }
    

    这些仍然作为前缀位置,但具有更高的优先级。

    【讨论】:

      【解决方案2】:

      这是我尝试过的,它奏效了。从这个帖子得到帮助 nginx - serve only images

      #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       9443;
              ssl on;
              ssl_certificate /path/to/ssl_certificate;        # path to your cacert.pem
              ssl_certificate_key /path/to/ssl_certifiatekey;    # path to your privkey.pem
              server_name  localhost;
      
              #charset koi8-r;
      
              #access_log  logs/host.access.log  main;
      
              location ~* \.(jpg|jpeg|gif|png|bmp|ico|pdf|flv|swf|exe|html|htm|txt|css|js)$ {
                  root /path/to/static/files;
                  expires 30d;
              }
      
              location / {
                  proxy_pass https://localhost:8443; #nodejsapp1
                  proxy_http_version 1.1;
                  proxy_set_header Upgrade $http_upgrade;
                  proxy_set_header Connection 'upgrade';
                  proxy_set_header Host $http_host;
                  proxy_cache_bypass $http_upgrade;
                  proxy_set_header X-Real-IP $remote_addr;
                  proxy_buffering         on;
              }
      
      
              location ~* /app1/ {
                proxy_pass https://localhost:8143; #nodejsapp2
                error_page 502 = @fallback;
              }
      
              location ~* /app2 {
                proxy_pass https://localhost:8343; #nodejsapp3
                error_page 502 = @fallback;
              }
      
              location @fallback{
                  rewrite ^ /maintenance;
                  proxy_pass https://localhost:8443;
              }
      
      
              #error_page  404              /404.html;
      
              # redirect server error pages to the static page /50x.html
              #
              error_page   500 502 503 504  /50x.html;
              location = /50x.html {
                  root   html;
              }
      
      
          }
      
      
      
          include servers/*;
      }
      

      【讨论】:

        猜你喜欢
        • 2020-03-07
        • 2018-02-03
        • 2016-11-16
        • 1970-01-01
        • 1970-01-01
        • 2019-05-17
        • 2018-01-28
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多