简体   繁体   English

从 nginx try_files 中排除文件夹

[英]Exclude a folder from nginx try_files

I have two locations being served by nginx.我有两个位置由 nginx 服务。 I'd like all the /api/* paths to be server by uwsgi, and have all other / paths served by index.html.我希望所有/api/*路径都由 uwsgi 提供服务器,并让 index.html 提供所有其他/路径。 I'm using Vue Router, so I also need this try_files $uri $uri/ /index.html;我正在使用 Vue Router,所以我还需要这个try_files $uri $uri/ /index.html;

Here's my full config which works for /, but does not properly exclude /api这是我的完整配置,适用于 /,但没有正确排除 /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;
    }
}

You will need configure an proxy in NGINX.你需要在 NGINX 中配置一个代理。 What you need to do is, run normally your application with npm run dev, npm start or another way you want.您需要做的是,使用 npm run dev、npm start 或其他您想要的方式正常运行您的应用程序。

Then redirect with location with proxy_pass.然后使用proxy_pass 重定向位置。 If you use 0.0.0.0 in your app, you can access from any address.如果您在应用中使用 0.0.0.0,则可以从任何地址访问。

See if my script might be useful to you.看看我的脚本是否对你有用。

#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/*;
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM