3

I'm using Ngnix the following configuration :

upstream sync {                                                                 
    server couchbase-sync:4984;                                                 
}                                                                               

server {                                                                        
    listen 4984;                                                                
    server_name ${WEBAPP_HOST};                                                 
    add_header 'Access-Control-Allow-Origin' '*';                               
    add_header 'Access-Control-Allow-Methods' '*';                              
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';                                    

    location / {                                                                
        if ($request_method = OPTIONS) {                                       
            return 204;                                                        
        }                                                                      
        proxy_pass http://sync;                                                 
    }                                                                           
}                                                                               

I do not want to redirect OPTIONS requests but I have a error :

nginx_1 | 2017/01/20 09:23:40 [emerg] 9#9: invalid condition "=" in /etc/nginx/conf.d/webapp.conf:13 nginx_1 | nginx: [emerg] invalid condition "=" in /etc/nginx/conf.d/webapp.conf:13

How do I solve my problem? Official Doc

Thank you for reading

Alex Lévy
  • 656
  • 1
  • 8
  • 15
  • 2
    Is this file post-processed? Maybe the `$request_method` string has become mangled. Try `nginx -T` to see what `nginx` is seeing. – Richard Smith Jan 20 '17 at 11:25

2 Answers2

1

The best practice for limiting http methods in nginx:

limit_except GET {
    allow 192.168.1.0/24;
    deny  all;
}

The above example will only allow everything for provided IP Range. And for the rest of clients only GET method is available.

You can use it in location blocks.

Methods available:

GET, HEAD, POST, PUT, DELETE, MKCOL, COPY, MOVE, OPTIONS, PROPFIND, PROPPATCH, LOCK, UNLOCK, or PATCH
Farhad Farahi
  • 35,528
  • 7
  • 73
  • 70
0

In my case some there was some problems with characters, as Richard Smith said. I had this problem when I was creating nginx.conf file at docker container. I was using echo command in bash, and then pasted copied text.

The problem disappeared when I created it with nano (pasted and saved).

TheKingElessar
  • 1,654
  • 1
  • 10
  • 30