Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$http_origin in Nginx config files

Tags:

nginx

I've seen it used in a lot of examples relating to CORS but where is the value of $http_origin set?

I checked the Nginx variables but there's no mention of it. And there's no mention in PHP FastCGI.

like image 314
Carlton Avatar asked Jul 31 '19 16:07

Carlton


People also ask

Where is nginx config file located?

Every NGINX configuration file will be found in the /etc/nginx/ directory, with the main configuration file located in /etc/nginx/nginx. conf . NGINX configuration options are known as “directives”: these are arranged into groups, known interchangeably as blocks or contexts .

What is Add_header in nginx?

Definition of Nginx Add_header. Nginx add_header allows us to define a value and an arbitrary response header is included in the code of the response. The nginx add_header is defined in the configuration file of nginx.

Has blocked by CORS policy no access control allow Origin header is present on the requested resource nginx?

This happens if you haven't set up CORS configuration correctly. you can fix this on you'r local machine using a plugin/extension called Allow-Control-Allow-Origin and add you'r localhost into it. The other way is to manually fix the configuration in server side.


2 Answers

$http_<headername> is automatically created based on the request headers. You can see the same on

https://nginx.org/en/docs/http/ngx_http_core_module.html#var_http_

$http_name

arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores

like image 95
Tarun Lalwani Avatar answered Sep 18 '22 19:09

Tarun Lalwani


If you look closer at http://nginx.org/docs/varindex.html, you'll notice there's only a single item with a prefix of $http_ mentioned on that page; and, in fact, this single item is lacking any further suffix, being generic on purpose.

The functionality is documented over at http://nginx.org/r/$http_ as follows:

$http_name — arbitrary request header field; the last part of a variable name is the field name converted to lower case with dashes replaced by underscores


However, if you look into the source code behind nginx, it does have internal optimisations for storing certain specific request headers in individual variables for performance reasons (these are defined as a list in the code below, but they are henceforth placed into a hash table during the initialisation process of nginx by ngx_http_variables_add_core_vars() function). For example, $http_host, $http_user_agent and $http_referer, plus a bunch of other variables that may have to be handled internally by certain nginx components, or which may be frequently used within the configuration files.

  • http://ngx.su/src/http/ngx_http_request.h#ngx_http_header_t
  • http://ngx.su/src/http/ngx_http_variables.c#ngx_http_core_variables
  • http://ngx.su/src/http/ngx_http_variables.c#ngx_http_variables_add_core_vars

However, as an end-user, you don't really have to worry about those kinds of details — the generic $http_name is sufficient to describe the whole functionality. (In fact, $http_origin isn't special or popular enough to have warranted its own specialised handling.)

like image 25
cnst Avatar answered Sep 17 '22 19:09

cnst