Home > nginx > Nginx, check for request method without using IF?

Nginx, check for request method without using IF?

October 3Hits:2

I am trying to optimize my Nginx config but ran into an issue I don't know how to fix. I have a folder /images on my server from which I deliver various static files for a website. The same folder though is also used in order to upload files via WebDAV.

Therefore, I have have to check for every request is the static file exists AND if the request method is GET, I deliver the file. Otherwise, I assume that the request was related to WebDAV (such as PUT, etc). See this sample:

if ($uri ~* ^/images)         { set $getimages "${getimages}A"; } if ($request_method = GET)    { set $getimages "${getimages}B"; } if ($getimages = "AB") {     rewrite ^ $uri$args last; } 

This above sample works fine. But I was wondering if there's another way to do this, without using IF. Given the IfIsEvil reference in the Nginx Wiki

Ideas anyone?


Re-post comment from Pothi as CW

If is not evil, if used outside a location block! Even inside a location block, it can be used with return and rewrite directives!

Snippet from IfIsEvil page

Directive if has problems when used in location context, in some cases it doesn't do what you expect but something completely different instead. In some cases it even segfaults. It's generally a good idea to avoid it if possible.

The only 100% safe things which may be done inside if in location context are:

return ...;
rewrite ... last;

Anything else may possibly cause unpredictable behaviour, including potential SIGSEGV.

Tags:http, nginx, webdav

Related Articles

  • Nginx, check for request method without using IF?October 3

    I am trying to optimize my Nginx config but ran into an issue I don't know how to fix. I have a folder /images on my server from which I deliver various static files for a website. The same folder though is also used in order to upload files via WebD

  • Nginx proxy by Request MethodJune 19

    Is it possible/how can I configure an Nginx location block to proxy to different backends depending on the request method (ie. GET/POST)? The reason is, I am currently handling the 2 methods at 2 different URLs (one via http proxy and the other via f

  • Use upstream servers based on request method with NGINXJanuary 19

    i'm using nginx's upstream directive to do some load balancing. But i want to handle the POST requests and the requests for /upload directory by my main server. how can i tell nginx "dont proxy pass if the request method is POST or the url contains /

  • Apache and Nginx - Forwarding .htaccess requests to Apache?December 3

    A really simple one but it's alluding me entirely. I've set up nginx as a proxy for Apache and at the moment it's only set to allow requests for certain files through to Apache, the config line is below. Problem I'm having, I need it to send .htacces

  • Nginx Retry of Requests ( Nginx - Haproxy Combination )March 11

    I wanted to ask about Nginx Retry of Requests. I have a Nginx running at the backend which then sends the requests to HaProxy which then passes it on the web server and the request is processed. I am reloading my Haproxy config dynamically to provide

  • What could be using an HTTP request method of "-"February 6

    Got an apache log entry that looks like this, actually I have a bunch of them spread out over time. XXX.XXX.XXX.XXX - - [27/Apr/2012:14:39:52 -0500] "-" 408 - "-" "-" It wasn't a GET or POST, the method was "-". Any

  • Critique Request: PHP Request-Method ClassAugust 6

    I'm working on a general Requestmethod class which sanitizes and recasts the input of users in an automatic fashion. I've also tried to do array-access in cookies. My biggest question are: Is this too much for one class alone? Is the readability/thin

  • Nginx, awstats and request time

    Nginx, awstats and request timeOctober 4

    How can I tune nginx to write request time to logs such that awstats can show time taken for each request? Also, how can I see requests by time taken in awstats? This is the current format log_format main '$remote_addr - $remote_user [$time_local] "$

  • Apache 2.2 Change HTTP Request Method From POST to GETDecember 5

    Is there a way I can change the HTTP Request Method from POST to GET using apache modules such as mod_rewrite or 3rd party module ? The reason I'm doing this is to minimize server side changes as I'm doing this for a demo.

  • Change the response body in apache based on request methodNovember 1

    I have an apache server that I'd like to configure to behave differently when the request method is OPTIONS. This is a static site so doing it in a scripting language isn't possible. In my case when the request method is OPTIONS I'd like apache to si

  • share code between check and process methodsDecember 11

    My job is to refactor an old library for GIS vector data processing. The main class encapsulates a collection of building outlines, and offers different methods for checking data consistency. Those checking functions have an optional parameter that a

  • Wireshark HTTP request methods for GET and POST on wlan0

    Wireshark HTTP request methods for GET and POST on wlan0August 2

    I can't seem to get the right information using Wireshark. I'm capturing on my wireless network, and I want to be able to inspect packets coming from users on my network. After capturing, I apply this filter: http.request.method == "GET" || http

  • What is HTTP COOK request method in my logs?March 1

    I'm seeing entries in my Apache logs like the following - - [24/Feb/2014:11:46:40 -0500] "COOK /freesearch.php?portal=0a9&... HTTP/1.0" 303 589 "-" "Mozilla/4.0 (compatible; Synapse)" with COOK in place of

  • Nginx forward all requests to another domainOctober 24

    I have been struggling to get this whole setup to work. I've found bits and pieces on the net but nothing that solves all of my requirements. I need to do this to make IE8 work, so unfortunately my hands are tied a little. I can move some endpoints a

  • axios request method DELETE use query string not body dataJanuary 20

    I'm using axios in the browser to communicate with my restful api server(django). When I am developing the Request method 'DELETE', I can't get data in the api server, and then I find that axios impelements DELETE method like GET with query string, n

  • How can I put a check on controller method 'Create' to avoid the error "Couldn't find Article with 'id'=" in Ruby on RailsJanuary 20

    How can I put a check on controller method 'Create' to avoid the error "Couldn't find Article with 'id'=" as I want to use create function for both Articles and Posts but here i am commenting from Posts and getting the error, I did try to use if

  • HTTP Status 405 - Request method 'POST' not supported Spring Security Java ConfigJanuary 23

    Hello I am facing the 'HTTP Status 405 - Request method 'POST' not supported' exception when trying to do login using spring security. Following is my code : pgLogin.jsp <%@ page language="java" contentType="text/html; charset=ISO-8859-1

  • Spring Security 4 : Request method 'POST' not supportedJanuary 24

    The issue is that when I click on submit I get Etat HTTP 405 - Request method 'POST' not supported Here is the controller : @RequestMapping(value = "/admin/login", method = RequestMethod.GET) public ModelAndView login(@RequestParam(value = "

  • HttpRequestMethodNotSupportedException: Request method 'POST' not supportedJanuary 25

    Creating a unit test with MockMvc I am running into: HttpRequestMethodNotSupportedException: Request method 'POST' not supported Which causes the test case to fail expecting a '200' but getting a '405'. Some of the additional things you may see in th

  • Android HttpURLConnection set GET Request methodFebruary 1

    I would like to send HTTP Request with GET method, but I can't set the GET method. Here's my code: try { URL url = new URL(path); conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequ

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.677 s.