I'm trying to cofigure nginx for reverse proxy in my ec2 server. My application starts with port 9000. So i added settings to nginx.conf file as below :
server {
listen 80;
location / {
proxy_pass http://localhost:9000;
}
}
After this i did 'service nginx start' and no response message, not even an error. I tried 'service nginx restart' and it is showing 'fail' message.
In my ec2 instance, i've port 80 listening to all requests.
Edit
I have bitnami configured on my instance, so it has apache as web server running. I don't want to disturb the current configuration. How could i do this with apache2 ?
For nginx, please try this bare-minimum configuration:
server {
listen 80;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:9000;
proxy_read_timeout 90;
}
}
As my instance is pre-configured by bitnami, i used apache for reverse proxy. I added the following to the httpd.conf file and it worked like a charm.
<VirtualHost *:80>
Servername domainname.com
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
</VirtualHost>
Thanks semm0 for the hints.
Related
I have application running on my AWS EC2 Instance's localhost:8080.
In order to use these Web portal, I have install nginx on EC2 and reversed proxy localhost so that I can access Web UI on my browser.
Nginx.conf file
server {
listen 80;
server_name ec2-xx-xx-xxx-xxx.eu-central-1.compute.amazonaws.com;
location / {
proxy_pass http://localhost:8080;
location /$request_uri {
proxy_pass http://localhost$request_uri;
}
}
When hitting EC2 URL on my browser,I successfully seeing homepage of application.
But when I hit any url let's say /admin, Nginx redirects to my local computer's localhost:8080/admin not Server's localhost.
All i want is that when hit any url nginx should forward the request to localhost:8080{$URL} and return me the browser.
please suggest where I'm wrong.
Thanks In Advance.
You don't need to add the second location.
location / {
proxy_pass http://localhost:8080;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
this should be enough to access anything you need.
I have a django app with gunicorn running on port 2333.In nginx.conf I set
server {
listen 80;
server_name mydomain.com;
location / {
proxy_cache my_cache;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://127.0.0.1:2333;
expires 30d;
}
now I can view my django app on address http://ipaddress:2333 and mydomain.com
but I don't want users to view my site by http://ipaddress:2333 .How to allow nginx only use mydomain.com to access my site.
I have tried to use "server default".It not worked.
server {
listen 2333 default;
server_name _;
return 500;
}
Nginx has nothing to do with that. Your Gunicorn (Django) app is listening on port 2333. Therefore, you can bypass nginx by connecting to http://$SERVER:2333. It will work even if you stop nginx.
What you need to do is tell gunicorn to listen only on the localhost, e.g. with --bind=127.0.0.1:2333. Then port 2333 will be accepting connections only from the local network interface.
I have a Django application that runs in a Docker environment; one container for gunicorn and one for nginx. My application's nginx server listens on port 9081, which is internal to the system (it's not exposed to the outside world). Another nginx container (which routes traffic) sits on port 80 and sends traffic to my site as necessary (based on the hostname a request receives).
Here's my application's nginx setup, stripped down to the basics:
upstream project {
server gun_project:8001; # gunicorn container
}
server {
listen 9081;
server_name mytool.myhost.com;
set_real_ip_from 172.17.0.0/16;
real_ip_header X-Forwarded-For;
real_ip_recursive on;
location / {
proxy_pass http://project;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Here's the router nginx setup, again stripped down:
upstream project {
server ngx_project:9081; # nginx container
}
server {
listen 80;
server_name mytool.myhost.com;
return 302 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name mytool.myhost.com;
# SSL Info
ssl_certificate /etc/nginx/ssl/mycert.cer;
ssl_certificate_key /etc/nginx/ssl/mycert.key;
location / {
proxy_pass http://project;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
I want to redirect a URL on this site from one location to another (the URL has permanently changed). I'm doing so via a rewrite in the location block of my application's nginx configuration (the first nginx block above):
location / {
rewrite "^/oldpath/$" /newpath/ permanent;
proxy_pass http://project;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
When I do this, and I attempt to load the old URL (mytool.myhost.com/oldpath/) in a web browser, I'm redirected to mytool.myhost.com:9081/newpath/ which fails because it doesn't exist (that port isn't exposed externally).
Is there something basic I'm missing? I don't want that internal port to be a part of the redirect.
Here's how I ended up doing it:
I added a dedicated location in the nginx configuration for the site, and performed the redirect there:
# Redirect the previous URL to the newer one
location = /old-path/ {
return 302 https://$host/new-path/;
}
I have a Ubuntu 13.04 virual machine with nginx frontend server and apache backend
serving a django project. Apache itself seems to work fine when operating separately
from nginx(and vice versa). But it looks like requests are not passed by nginx to
Apache when they work together.
After typing in my_host_name.com in the browser i get nginx "Welcome....!" page,
but it should be an apache-rendered page instead.
UPD: the "Welcome..." page appears only on first load, after the browser(chrome) is restarted(caching?), otherwise it returns just an empty page ("This webpage is not available .......... Error code: ERR_CONNECTION_RESET "). I get the same error page immediatly when accessing _my_domain_name.com_ from the host OS. I restart both servers(just in case) after each configuration change.
Typing in my_domain_name.com:8000 returns a correct apache response(static files
excluded)
After a day of googling still can't find what's wrong.
My servers' settings are:
File: /etc/nginx/proxy_params
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
File: /etc/nginx/sites-available/my_domain_name
server {
listen 80;
server_name my_domain_name.com;
location / {
access_log /var/log/nginx/localhost.access.log;
proxy_pass http://127.0.0.1:8000;
include /etc/nginx/proxy_settings;
}
location /static/ {
root /path/to/my/project/root;
}
location /media/{
root /path/to/my/project/root;
}
}
File: /etc/apache2/sites-available/my_domain_name.conf
<VirtualHost *:8000>
WSGIScriptAlias / /path/to/my/project/root/django.wsgi
ServerName my_domain_name.com
<Directory /path/to/my/project/root>
Order allow,deny
Allow from all
</Directory>
LogLevel warn
CustomLog /var/log/apache2/access.log combined
RPAFenable On
RPAFsethostname On
RPAFproxy_ips 192.168.137.10 127.0.0.1
</VirtualHost>
File: /etc/apache2/ports.conf
NameVirtualHost *:8000
Listen 8000
File: /etc/hosts
192.168.137.10 my_domain_name.com
127.0.0.1 my_domain_name.com
Thanks!
I was having a similar issue with my Nginx setup with my Apache back end, specifically when Apache was issuing a redirect for:
/some-location => /some-location/
Nginx was running on port 80, and my back end Apache on port 8000. and port 8000 was being passed onto the client in the 301 redirect.
It took a bit of playing, but I was able to get it to work. Here is what I had to do:
location / {
proxy_pass http://127.0.0.1:8000;
proxy_redirect default;
proxy_redirect http://$host:8000/ http:/$host/;
... etc ..
}
Both Nginx and Apache virtual servers are using the same hostname (i.e. mydomain.com)
I'm developing with apache2 ( mpm-worker ) + mod_wsgi behind nginx which is silly since I have to sudo apache2ctl graceful for every update I make in anything but the template files.
My nginx conf is:
server {
listen 80;
server_name site.org;
access_log /www/site.org/log/access.log;
error_log /www/site.org/log/error.log;
location / {
proxy_pass http://127.0.0.1:8080/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Magic-Header "secret";
client_max_body_size 10m;
}
}
Would it be a matter of just binding proxy_pass to 127.0.0.1:3000 if 3000 is the port used by the django server?
Ack, didn't realize it was this easy... I..
copied the server {} settings into another file
changed the port to 3001
changed the server name to dev.site.org
updated my host records in the DNS to point to my server IP
restarted nginx
did manage.py runserver 3001.
All is well :)