DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Error Handling Inside Kumologica Subflow
  • Serverless Patterns: Web
  • Watching the Requests Go By: Reconstructing an API Spec with APIClarity
  • About Using Regexp in Nginx Map

Trending

  • Microsoft Azure Synapse Analytics: Scaling Hurdles and Limitations
  • Unlocking AI Coding Assistants Part 4: Generate Spring Boot Application
  • A Guide to Developing Large Language Models Part 1: Pretraining
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot

Request Processing Phases in Nginx. Is if Evil?

Difficulties in working with the order of request processing phases.

By 
Alexey Shepelev user avatar
Alexey Shepelev
DZone Core CORE ·
Oct. 23, 21 · Code Snippet
Likes (3)
Comment
Save
Tweet
Share
5.0K Views

Join the DZone community and get the full member experience.

Join For Free

The worst evil in Nginx is if when used in location context. Much has been written about this, including posts on nginx.com. Let’s take a quote:

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

- return ...;

- rewrite ... last;

It seems that nothing terrible will happen if you use a construction like

Plain Text
 
location / {
  if ($condition) {
    return 418;
  }
  ...
}

However, with a certain “skill”, you can even break something that is 100% safe. But will if be responsible for this malfunction?

Let’s assume we have a web server that accepts POST requests:

Plain Text
 
server {
    root /www/example_com;
    listen *:80;
    server_name .example.com;
        
    location /index.php {
        fastcgi_pass   unix:/var/run/php-fpm.sock;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}
Shell
 
$ cat /www/example_com/index.php
<?php
var_dump($_REQUEST);
?>
Shell
 
$ curl \
> -w "HTTP CODE: %{http_code}\n" \
> -d "key1=value1" \
> -X POST \
> "example.com/index.php" \
> 
array(1) {
  ["key1"]=>
  string(6) "value1"
}
HTTP CODE: 200

At some point, we want to filter out requests with empty bodies. The first thing we can think of makes the config look like this:

Plain Text
 
server {
  root /www/example_com;
  listen *:80;
  server_name  .example.com;
        
  location /index.php {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;

    if ($request_body = '') {
      return 418;
    }
  }
}

However, this idea is a mistake. The fact is that in this configuration, we will always (no matter the body is sent or not) get the 418th response:

Shell
 
$ curl \
> -w "HTTP CODE: %{http_code}\n" \
> -X POST \
> "example.com/index.php" \
> 
HTTP CODE: 418
Shell
 
$ curl \
> -w "HTTP CODE: %{http_code}\n" \
> -d "key1=value1" \
> -X POST \
> "example.com/index.php" \
> 
HTTP CODE: 418

How phases are defined in Nginx:

C
 
typedef enum {
    NGX_HTTP_POST_READ_PHASE = 0,
    NGX_HTTP_SERVER_REWRITE_PHASE,
    NGX_HTTP_FIND_CONFIG_PHASE,
    NGX_HTTP_REWRITE_PHASE,
    NGX_HTTP_POST_REWRITE_PHASE,
    NGX_HTTP_PREACCESS_PHASE,
    NGX_HTTP_ACCESS_PHASE,
    NGX_HTTP_POST_ACCESS_PHASE,
    NGX_HTTP_PRECONTENT_PHASE,
    NGX_HTTP_CONTENT_PHASE,
    NGX_HTTP_LOG_PHASE
} ngx_http_phases;

Our condition uses the variable $request_body, whose value appears in locations processed by proxy_pass, fastcgi_pass, uwsgi_pass, and scgi_pass directives when the body is read into an in-memory buffer.

Therefore, the value of the variable is set in the NGX_HTTP_CONTENT_PHASE phase, while the if directive of the ngx_http_rewrite_module module is executed in the NGX_HTTP_REWRITE_PHASE phase. That is, at the moment of checking the condition, the $request_body variable will not be assigned any value regardless of whether the body is empty or not. Therefore, any queries will be responded to with"418 I’m a teapot".

Thus, although the 100% efficient version is no longer working, if in location context has nothing to do with it. This is only about the order of request processing phases.

Requests Processing Plain text

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • Serverless Patterns: Web
  • Watching the Requests Go By: Reconstructing an API Spec with APIClarity
  • About Using Regexp in Nginx Map

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!