iT邦幫忙

0

nginx 跨域存取錯誤 405:Not Allowed

  • 分享至 

  • twitterImage

因為一直測試nginx跨域存取不斷的把設定檔越拉越長
到最後APP跳出錯誤405:Not Allowed,感覺上好像快成功了
在網路上找到一篇可以跳過這個錯誤
但是在開APP卻被檢查版本的功能擋下來,然後閃退
顯示請前往商店更新最新版本
但是同樣的版本在apache是沒問題的
https://blog.daliansky.net/an-nginx-405-not-allowed-error-solution.html
看來只剩下靜態網頁的post問題了


server {
listen 80;
server_name test.abc.com.tw
root   /var/www/html/tapi;
index index.php index.html index.htm;
access_log /var/log/nginx/api_access_log;
error_log   /var/log/nginx/api_error_log   error;
include dropfavicon.conf;

add_header Access-Control-Allow-Origin *;
location / {
root /var/www/html/tapi;

    if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
return 200;
    }

if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

  if (!-e $request_filename){
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    rewrite ^/(.+)/$ /$1 redirect;
  }
  if (!-e $request_filename){
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    rewrite ^(.*)$ /index.php?$1 break;
  }

 /403.html;
        # To allow POST on static pages 允许静态页使用POST方法
       error_page  405     =200 $uri ;

index index.html index.php index.htm
autoindex off;
allow all;
try_files $uri $uri/ /index.php?$query_string;
}

add_header Access-Control-Allow-Origin *;

location ~ \.php$ {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT,DELETE, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    rewrite ^/(.+)/$ /$1 redirect;

if ($request_method = 'OPTIONS') {
        add_header Access-Control-Allow-Origin *;
        add_header Access-Control-Allow-Methods GET,POST,PUT,DELETE,OPTIONS;
        return 204;
    }
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}

 if (!-e $request_filename){
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    rewrite ^/(.+)/$ /$1 redirect;
  }

  if (!-e $request_filename){
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET,POST,PUT,DELETE,OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-Mx-ReqToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    rewrite ^(.*)$ /index.php?$1 break;
  }

autoindex off;
allow all;
root    /var/www/html/tapi;
fastcgi_pass   127.0.0.1:9001;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include         fastcgi_params;
include /etc/nginx/fastcgi_params;
}
}

hsiang11 iT邦好手 1 級 ‧ 2018-03-20 15:26:26 檢舉
log的可能錯誤
2018/03/20 15:10:14 [emerg] 3788#0: named location "@405" can be on the server level only in /etc/nginx/conf.d/api.conf:57

這剛好又是靜態POST的那行
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
bizpro
iT邦大師 1 級 ‧ 2018-03-20 22:16:25

答案正是:
named location "@405" can be on the server level only
405是location, 您的405是放在
location / {...}
之內, 也就是說您有階層式location!
應該"直接"放在server之下, 如:

server {
  ...
  location / {...}
  ...
  error_page 403 /403.html
  error_page 404 /404.html
  # To allow POST on static pages 允许静态页使用POST方法
  error_page  405     =200 $uri ;
  ...
}

我要發表回答

立即登入回答