I am attempting to convert all traffic to its "matching" internal equivalent, and proxy the traffic via this new internal URL.
For example,
http://external.com/image/blue.png will map to http://image.internal.com/blue.png
I have followed the guide here to attempt to set this up via nginx, however I am receiving the following error:
nginx: [emerg] unknown "path" variable
Below is my nginx configuration file:
server {
listen 80;
listen [::]:80;
root /var/www/private/$subdomain;
index index.html index.htm index.nginx-debian.html;
server_name external.com;
location ~ ^/(?<subdomain>[^/]+)/(<path>.*)?$ {
proxy_pass http://$subdomain.internal.com/$path;
proxy_buffering off;
proxy_set_header Host $http_host;
}
}
Do I need to define the 'path' variable somewhere else? All help is appreciated, thank you.
You're missing a question mark. It's ?<path> not <path>
Related
I have reconfigured nginx but i can't get it to restart using the following config:
conf:
server {
listen 80;
server_name www.example.com;
return 301 $scheme://example.com$request_uri;
}
server {
listen 80;
server_name example.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
location /robots.txt {
alias /path/to/robots.txt;
access_log off;
log_not_found off;
}
location = /favicon.ico { access_log off; log_not_found off; }
location / {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_connect_timeout 30;
proxy_read_timeout 30;
proxy_pass http://127.0.0.1:8000;
}
location /static {
expires 1M;
alias /path/to/staticfiles;
}
}
after running sudo nginx -c conf -t to test the configuration the following error is returned i can't figure out what is really the problem
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-available/config:1
nginx: configuration file /etc/nginx/sites-available/config test failed
That is not an nginx configuration file. It is part of an nginx configuration file.
The nginx configuration file (usually called nginx.conf) will look like:
events {
...
}
http {
...
server {
...
}
}
The server block is enclosed within an http block.
Often the configuration is distributed across multiple files, by using the include directives to pull in additional fragments (for example from the sites-enabled directory).
Use sudo nginx -t to test the complete configuration file, which starts at nginx.conf and pulls in additional fragments using the include directive. See this document for more.
Example valid nginx.conf for reverse proxy; In case someone is stuck like me.
where 10.x.x.x is the server where you are running the nginx proxy server and to which you are connecting to with the browser, and 10.y.y.y is where your real web server is running
events {
worker_connections 4096; ## Default: 1024
}
http {
server {
listen 80;
listen [::]:80;
server_name 10.x.x.x;
location / {
proxy_pass http://10.y.y.y:80/;
proxy_set_header Host $host;
}
}
}
Here is the snippet if you want to do SSL pass through. That is if 10.y.y.y is running a HTTPS webserver. Here 10.x.x.x, or where the nignx runs is listening to port 443, and all traffic to 443 is directed to your target web server
events {
worker_connections 4096; ## Default: 1024
}
stream {
server {
listen 443;
proxy_pass 10.y.y.y:443;
}
}
and you can serve it up in docker too
docker run --name nginx-container --rm --net=host -v /home/core/nginx/nginx.conf:/etc/nginx/nginx.conf nginx
The path to the nginx.conf file which is the primary Configuration file for Nginx - which is also the file which shall INCLUDE the Path for other Nginx Config files as and when required is /etc/nginx/nginx.conf.
You may access and edit this file by typing this at the terminal
cd /etc/nginx
/etc/nginx$ sudo nano nginx.conf
Further in this file you may Include other files - which can have a SERVER directive as an independent SERVER BLOCK - which need not be within the HTTP or HTTPS blocks, as is clarified in the accepted answer above.
I repeat - if you need a SERVER BLOCK to be defined within the PRIMARY Config file itself than that SERVER BLOCK will have to be defined within an enclosing HTTP or HTTPS block in the /etc/nginx/nginx.conf file which is the primary Configuration file for Nginx.
Also note -its OK if you define , a SERVER BLOCK directly not enclosing it within a HTTP or HTTPS block , in a file located at path /etc/nginx/conf.d . Also to make this work you will need to include the path of this file in the PRIMARY Config file as seen below :-
http{
include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}
Further to this you may comment out from the PRIMARY Config file , the line
http{
#include /etc/nginx/sites-available/some_file.conf; # Comment Out
include /etc/nginx/conf.d/*.conf; #includes all files of file type.conf
}
and need not keep any Config Files in /etc/nginx/sites-available/ and also no need to SYMBOLIC Link them to /etc/nginx/sites-enabled/ , kindly note this works for me - in case anyone think it doesnt for them or this kind of config is illegal etc etc , pls do leave a comment so that i may correct myself - thanks .
EDIT :- According to the latest version of the Official Nginx CookBook , we need not create any Configs within - /etc/nginx/sites-enabled/ , this was the older practice and is DEPRECIATED now .
Thus No need for the INCLUDE DIRECTIVE include /etc/nginx/sites-available/some_file.conf; .
Quote from Nginx CookBook page - 5 .
"In some package repositories, this folder is named sites-enabled, and
configuration files are linked from a folder named site-available;
this convention is depre‐ cated."
There might be just a typo anywhere inside a file imported by the config. For example, I made a typo deep inside my config file:
loccation /sense/movies/ {
mp4;
}
(loccation instead of location), and this causes the error:
nginx: [emerg] "server" directive is not allowed here in /etc/nginx/sites-enabled/xxx.xx:1
Replace include /etc/nginx/conf.d/*.conf; in nginx.conf with include /etc/nginx/conf.d/includes-optional/cpanel-proxy-vendors/*.conf; or /etc/nginx/conf.d/includes-optional/site-available/*.conf;
In my nginx server i set my nginx.conf like this for using with gunicorn (and a django app):
server {
server_name *.mytest.io;
root /var/www;
}
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name ~^(?<account>.+)\.mytest\.io$;
root /var/www;
#add_header Test $account;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /static/ {
alias /var/www/web/core/frontend/static/;
}
location / {
proxy_pass http://127.0.0.1:8000;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header X-Real-IP $remote_addr;
#add_header P3P 'CP="ALL DSP COR PSAa PSDa OUR NOR ONL UNI COM NAV"';
add_header P3P $account;
}
now if i call mytest.io all is done, but if i call for example demo.mytest.io or demo1.mytest.io (i already configure on my dns demo and demo1 with the server ip) nginx return the error "403 Forbidden"
[error] 27554#0: *27 directory index of "/var/www/" is forbidden
Why for main domain there are no problem and for subdomain request i get an error on the same location? (/var/www)
Thanks in advance
You have two server blocks. A request to demo.mytest.io or demo1.mytest.io will match the server_name directive in this one:
server {
server_name *.mytest.io;
root /var/www;
}
You have no index or try_files directives in this block, so a request without a uri path is interpreted by Nginx as a request for the directory listing of your root directory.
You don't have an autoindex directive, and by default the value of this is set to off which prevents the listing of directory contents, so your request is denied.
A request to mytest.io doesn't match the server_name directive in either block, but the addition of default_server to the listen directive in the second block means Nginx will use this block to process any requests which do not match any other server blocks, so that request lands here.
The second block has a working set of directives, so it works.
I'm having a bit of a problem with NGINX. I made a simple HTML that loads my application in a iframe hosted on http://localhost:3000.
Using the config below, localhost:3000 is proxied but root /var/www is ignored so I only get to see the application without any of my html.
What would be the correct way to proxy a url loaded inside an iframe?
server {
listen 80;
listen [::]:80;
root /var/www/sub.mydomain.com/html;
index index.html index.htm index.nginx-debian.html;
server_name sub.mydomain.com;
location / {
proxy_set_header Host $host;
proxy_pass http://localhost:5601/;
}
}
I am configuring Nginx load balance with Nginx upstream module, configuration as follow:
upstream load {
server loadapi.example.com;
server loadapi.anotherdomain.com down;
}
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://load;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 80;
server_name loadapi.example.com;
root /disk/projects/load/loadapi;
index index.html index.htm index.shtml index.php;
...
...
error_page 404 /404.html;
}
Notice that the api.example.com and loadapi.example.com are on the same server. loadapi.anotherdomain.com is resolved to another server which provides the same service.
Everything works fine with loadapi.anotherdomain.com, which are on another server.
But when I use the loadapi.example.com as the backend, it seems that Nginx cannot handle it correctly. I can get my service up and running on loadapi.example.com. But it is not working with the upstream.(look like Nginx cannot resolve the subdomain name correctly).
any advice? thx in advance.
nginx uses the Host header to determine which server block to use to process a request.
When the request passes through the proxy_pass http://load; statement, the Host header is set to the value load by default.
To make nginx choose the server block containing the server_name loadapi.example.com; statement, it either needs to be the default_server server, or include the name load in its server_name, or set the Host header using:
proxy_set_header Host loadapi.example.com;
Of course, using upstream for load balancing means that both servers receive the same value for the Host header, and must both respond correctly to it.
See this document for more.
I have NGINX running on port 8080. I have the following setup in my NGINX conf file.
server {
listen 8080;
server_name domain.com;
location / {
#root /usr/share/nginx/html;
#index index.html index.htm;
proxy_pass http://127.0.0.1:80;
proxy_redirect http://127.0.0.1:80 http://domain.com;
}
These rules work correctly as far as I can tell. The only issue I run into is when Pylons gets a request for a relative URL it is using http://127.0.0.1/linkto/something instead of http://domain.com:8080/linkto/something. I believe I am missing something in my Pylons configuration, if you have any advice or need additional information just let me know. Thanks in advance for any assistance on this.
By default, proxy_pass uses the hostname from the directive (127.0.0.1 in your case) as the Host: header for its request. You probably just need to add proxy_set_header Host $http_host; to have it pass through the original Host header to your backend.