Right now I have set up NGINX and had a question regarding the url paths and how NGINX approaches it. Right now, if I do something like /sample.html, it will effectively display sample.html. However, I want to do something where irregardless of what the user inputs in the URL, it displays sample.html. For example, doing something like /network will still display sample.html but will retain the url path of /network. Right now, this is how my NGINX.conf looks like:
worker_processes 2;
pid logs/nginx.pid;
error_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=error;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
access_log syslog:server=unix:/dev/log,facility=local7,tag=nginx,severity=info combined;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root /home/user/Downloads/nginx/html;
location /network {
index sample.html sample.htm;
}
location / {
index sample.html sample.htm;
autoindex on;
}
#error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
I thought this would work but it is just returning a 404 error. Any idea on how I can change this so that it would work in how I want it to work, if it is possible?
Thank you, and let me know if you need more information!
You could use try_files to accomplish this within a location block. See http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files
location / {
try_files $uri $uri/ /sample.html;
}
Related
I am facing problems launching my django app in digitalocean. I have done all the needful but I am quite sure I made error some where. Below is my nginx configuration file. What could be the problem . Thanks alot .
PLease I would also love to know how to access nginx error log file through command line.
server {
server_name server_Ip;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/username/myproject/smsdigo;
}
location /media/ {
root /home/username/myproject/smsdigo;
}
location /locale/ {
root /home/ebong/myproject/smsdigo;
}
location / {
include proxy_params;
proxy_pass http://unix:/run/gunicorn.sock;
}
}
server {
listen 80;
client_max_body_size 3000M;
client_body_buffer_size 3000M;
client_body_timeout 120;
}
Problably you need increase fastcgi timeout directives:
Try 60s or more. For example:
server{
fastcgi_read_timeout 60s;
fastcgi_send_timeout 60s;
}
I have a centOS with a virtualenv, i can use the project in my localhost , but when the project is upload to a server give an error :
502 - bad gateway
I think the problem probably is in my nginx file.
server {
listen 80;
server_name www.site.com.br site.com.br;
root /var/www/html/agrodez/src/;
if ($http_host != "www.site.com.br") {
rewrite ^ http://site.com.br$request_uri permanent;
}
location /static/ {
alias /var/www/html/site/src/sistema/static/;
}
location /{
proxy_pass http://localhost:8000;
include /etc/nginx/proxy_params;
}
I don't know much about django, but recently I had to help a team with server configuration, and I've used the following nginx virtualhost configuration:
upstream django.local {
server 127.0.0.1:8000;
}
server {
listen 80;
server_name site.com;
location / {
root html;
index index.html index.htm;
proxy_pass http://django.local/;
}
location /static {
autoindex on;
# this line would be STATIC_ROOT dir
alias /var/www/html/agrodez/src/staticfiles;
}
location /media {
autoindex on;
# this line would be MEDIA_ROOT dir;
alias /var/www/html/agrodez/src/media;
}
# If you want nginx logs, then can add these
error_log /var/log/nginx/site.com_error.log;
access_log /var/log/nginx/site.com_access.log;
}
You can give it a try.
I am writing a Django app which uses an nginx reverse proxy + gunicorn as a webserver in production.
I want to include the capability to stop DDOS attacks from a certain IP (or pool of IPs). This as to be at the nginx level, rather than any deeper in the code. Do I need a web application firewall? If so, how do I integrate it.
My project's nginx file located at sites-available has:
server {
listen 80;
charset utf-8;
underscores_in_headers on;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/sarahm/djangoproject/djangoapp;
}
location /static/admin {
root /home/sarahm/.virtualenvs/myenv/local/lib/python2.7/site-packages/django/contrib/admin/static/;
}
location / {
proxy_pass_request_headers on;
proxy_buffering on;
proxy_buffers 8 24k;
proxy_buffer_size 2k;
include proxy_params;
proxy_pass http://unix:/home/sarahm/djangoproject/djangoapp/djangoapp.sock;
}
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/sarahm/djangoproject/djangoapp/templates/;
}
}
Let me know if I should include more information, and what that information should be.
If you want to prevent certain IPs or even subnets from accesing your app, add the following code to your server block:
#Specify adresses that are not allowed to access your server
deny 192.168.1.1/24;
deny 192.168.2.1/24;
allow all;
Also if you're not useing REST, then you might want to limit possible HTTP verbs, by adding the following to your server block:
if ($request_method !~ ^(GET|HEAD|POST)$ ) {
return 403;
}
To lessen the possibility of DoS attack, you might want to limit the number of possible requests from single host (see http://nginx.org/en/docs/stream/ngx_stream_limit_conn_module.html), by adding the following to NGINX nginx.conf:
limit_conn_zone $binary_remote_addr zone=limitzone:1M;
and the following to your server block:
limit_conn limitzone 20;
Some other useful setting for nginx.conf, that help mitigate DoS if set correctly:
server_tokens off;
autoindex off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
client_body_timeout 10;
client_header_timeout 10;
send_timeout 10;
keepalive_timeout 20 15;
open_file_cache max=5000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
Since it's too broad to explain these all here, suggest you to look in the docs http://nginx.org/en/docs/ for details. Though choosing correct values is achieved via trial and error on particular setup.
Django serves error pages itself as templates, so you should remove:
error_page 500 502 503 504 /500.html;
location = /500.html {
root /home/sarahm/djangoproject/djangoapp/templates/;
Adding access_log off; log_not_found off; to static if you don't really care for logging is also an option:
location /static/ {
access_log off;
log_not_found off;
root /home/sarahm/djangoproject/djangoapp;
}
this will lower the frequency of filesystem requests, therefore increasing performance.
NGINX is a great web server and setting it is a broad topic, so it's best to eaither read the docs (at least HOW-TO section) or find an article that describes the setup for a situation close to yours.
I have set up a server according to this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-14-04
Everything is working a-okay, but I would like to change my NGINX set up to incorporate AngularJS for the front end. Right now I have it configured as the tutorial says and when I visit myip/ I get my Django app, and when I go to myip/static/ I get my static files. Great.
What I would like to do is serve the Django API from a api.myip subdomain, and have myip/ actually point to my static (angular app) files.
Any insight on how to configure NGINX to route this correctly?
NGINX Config currently looks like this:
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/user/myproject;
}
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
try like this
server {
listen 80;
server_name server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
root /home/user/myproject;
}
}
server {
listen 80;
server_name api.server_domain_or_IP;
location = /favicon.ico { access_log off; log_not_found off; }
location / {
include proxy_params;
proxy_pass http://unix:/home/user/myproject/myproject.sock;
}
}
With my web browser: when I go to my_domain.com => I am redirected to XX.XX.XX.XX (the IP)
and the address displayed in my web browser remains XX.XX.XX.XX.
I want to point on XX.XX.XX.XX and I want the address displayed my_domain.com (same behaviour as in any website).
My configuration file: /etc/nginx/nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
client_max_body_size 24M;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deploy/.rvm/gems/ruby-1.9.3-p547/wrappers/ruby;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
And this is my other configuration file /etc/nginx/sites-enabled/default
server {
listen 80;
server_name my_domain.com;
add_header X-Frame-Options "SAMEORIGIN";
location / {
root /home/deploy/www/comint/current/public;
passenger_enabled on;
rails_env development;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
allow ::1;
deny all;
}
}
when I try to perform a redirect, I have a loop error.
I tried many configurations, but it seems that I misunderstood something.
May someone explain me what the problem can be ?
Thank you
PS: I'm a rails developper, but that's my first web server configuration.