簡體   English   中英

從 nginx try_files 中排除文件夾

[英]Exclude a folder from nginx try_files

我有兩個位置由 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;
    }
}

你需要在 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/*;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM