如何在proxy_pass中正确使用nginx try_files?

时间:2019-06-29 13:16:26

标签: nginx proxypass

我正在尝试在Nginx proxy_pass中使用try_files。 如果我输入h--p://example.com/admin1/SisrAZbCCnisI,并且不使用try_fiels,那么一切都会正常进行。

  • [INFO]飞镖:GET / admin1 / SisrAZbCCnisI = 9ms 200
  • [INFO]飞镖:GET / admin1 / SisrAZbCCnisI = 63ms 200

如果我输入错误的网址,则会收到404错误。

  • [INFO]飞镖:GET / admin1 12ms 404
  • [INFO]飞镖:GET / admin1 / 12ms 404
  • [INFO]飞镖:GET / admin1 /错误字符串12ms 404

,当我尝试始终使用try_files时,我会得到默认的index.html。

如何在proxy_pass中正确使用nginx try_files?

更新: 错误页面,并且index.html在工作。只有我的proxy_pass无法正常工作。当我输入/ adnin1 / ***

时仍显示index.default
add_header X-Frame-Options SAMEORIGIN;
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";

server {
    charset UTF-8;
    listen 80;
    listen [::]:80;
    server_name  my.example.com;

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

    # redirect error pages to the static page
    error_page 401 402 403 405 404  /index.html;
    location = /index.html {
        root /usr/share/nginx/html;
        internal;
    }

    # redirect server error pages to the static page
    error_page   500 502 503 504  /index.html;
    location = /index.htm {
        root /usr/share/nginx/html;
        internal;
    }

    # deny access to .htaccess files
    location ~ /\.ht {
        deny  all;
    }

    # Deny access to hidden files (beginning with a period)
    location ~ /\. {
         deny all;
    }

   if ($host = my.example.com/admin1) {
         return 301 https://my.example.com$request_uri;
   }

   if ($host = my.example.com/admin2) {
         return 301 https://my.example.com$request_uri;
   }

}

server {
    charset UTF-8;
    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl http2; # managed by Certbot
    server_name  my.example.com;

    ssl_certificate /etc/letsencrypt/live/my.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/my.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

    access_log  /var/log/nginx/host.access.log  main;

    # redirect mobile app to
    location /admin1 {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass "http://127.0.0.1:3000";
        proxy_connect_timeout 1;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
        proxy_intercept_errors on;
    }

    # redirect mobile app to
    location /admin2 {
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass "http://127.0.0.1:3000";
        proxy_connect_timeout 1;
        proxy_next_upstream error timeout http_500 http_502 http_503 http_504 http_404;
        proxy_intercept_errors on;
    }

    # redirect error pages to the static page
    error_page 401 402 403 405 404  /index.html;
    location = /index.html {
        root /usr/share/nginx/html;
        internal;
    }

    # redirect server error pages to the static page
    error_page   500 502 503 504  /index.html;
    location = /index.htm {
        root /usr/share/nginx/html;
        internal;
    }

    # deny access to .htaccess files
    location ~ /\.ht {
        deny  all;
    }

    # Deny access to hidden files (beginning with a period)
    location ~ /\. {
         deny all;
    }

    if ($request_method !~ ^(GET|HEAD|POST)$ )
    {
    return 404;
    } 

}

0 个答案:

没有答案