【问题标题】:Exclude a folder from nginx try_files从 nginx try_files 中排除文件夹
【发布时间】:2020-04-07 10:06:57
【问题描述】:

我有两个位置由 nginx 提供服务。我希望所有/api/* 路径都由uwsgi 作为服务器,并让所有其他/ 路径由index.html 提供服务。我用的是Vue Router,所以我也需要这个try_files $uri $uri/ /index.html;

这是我的完整配置,适用于 /,但没有正确排除 /api

server {
    listen       8080;
    location ^~ /api {
        include uwsgi_params;
        uwsgi_pass localhost:9000;
    }

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

      location ~ ^(?!/api).+ { { # this doesn't work. i'm trying to ignore /api/* for the rule below.
        try_files $uri $uri/ /index.html;
      }
    }

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

【问题讨论】:

    标签: vue.js nginx vue-router


    【解决方案1】:

    您需要在 NGINX 中配置代理。 您需要做的是,使用 npm run dev、npm start 或其他您想要的方式正常运行您的应用程序。

    然后使用 proxy_pass 重定向位置。如果您在应用中使用 0.0.0.0,则可以从任何地址访问。

    看看我的脚本是否对你有用。

    #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 default_server;
            listen [::]:80 default_server;
            server_name localhost mydomain.com;
            # Discourage deep links by using a permanent redirect to home page of HTTPS site
            return 301 https://$host;
            # Alternatively, redirect all HTTP links to the matching HTTPS page 
            # return 301 https://$host$request_uri;
    
            # API 1
            location /api1 {
                proxy_pass http://0.0.0.0:8001;
            }
            #  APP 1
            location /app1 {
                proxy_pass http://0.0.0.0:3001;
            }
    
            # APP 2
            location /app2 {
                proxy_pass http://0.0.0.0:3002;
            }
    
            # API 2
            location /api2 {
                proxy_pass http://0.0.0.0:8002;
            }
    
        }
    
    
    
        # HTTPS server
        server {
            listen       443 ssl;
            listen 443 ssl default;
            server_name  localhost, mydomain.com;
            gzip  on;
            ssl_certificate /etc/letsencrypt/live/mydomain/fullchain.pem; # managed by Certbot
            ssl_certificate_key /etc/letsencrypt/live/mydomain/privkey.pem; # managed by Certbot
            ssl_session_cache    shared:SSL:1m;
            ssl_session_timeout  5m;
            ssl_ciphers  HIGH:!aNULL:!MD5;
            ssl_prefer_server_ciphers  on;
    
           # config to enable HSTS(HTTP Strict Transport Security)
           add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    
            # API 1
            location /api1 {
                proxy_pass http://0.0.0.0:8001;
            }
            #  APP 1
            location /app1 {
                proxy_pass http://0.0.0.0:3001;
            }
    
            # APP 2
            location /app2 {
                proxy_pass http://0.0.0.0:3002;
            }
    
            # API 2
            location /api2 {
                proxy_pass http://0.0.0.0:8002;
            }
        }
        include servers/*;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-30
      • 2013-05-17
      相关资源
      最近更新 更多