When I try restart nginx and write in console this command
nginx -t
I have an error:
nginx: [emerg] a duplicate default server for 0.0.0.0:80 in /etc/nginx/sites-enabled/default.save:20
nginx: configuration file /etc/nginx/nginx.conf test failed
sites-enabled/default
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name localhost;
passenger_enabled on;
rails_env production;
root /home/hh/public;
access_log /var/log/nginx/host.access.log;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
One hour ago everything worked fine, but after restart nginx I have this issue.
If you look at /etc/nginx/sites-enabled/ you see two files, default.save and default just remove one of them
sudo rm -rf /etc/nginx/sites-enabled/default.save
you should remove default_server on default conf file.
Related
Nginx runs fine with an empty sites-enabled directory, but then I create the symlink with
sudo ln -s /etc/nginx/sites-available/default /etc/nginx/sites-enabled/,
and restarting nginx fails. I type nginx -T, and the culprit is
2020/06/29 20:24:19 [emerg] 4087#4087: duplicate upstream "appname" in /etc/nginx/sites-available/default:1
, which is clearly caused my symolink, so I'm not sure what I did wrong.
The original problem I'm trying to fix is getting my EC2 instance to serve my API instead of the default "Welcome to nginx!" page.
Deleting the symlink lets nginx run normally. Here is a shortened version of my output of nginx -T when it works:
# configuration file /etc/nginx/nginx.conf:
user root;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
events {
worker_connections 20000;
# multi_accept on;
}
http {
disable_symlinks off;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
...
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
include /etc/nginx/sites-available/*;
...
}
# configuration file /etc/nginx/sites-available/default:
upstream appname {
# Path to Puma SOCK file, as defined previously
server unix:/home/ubuntu/appname/shared/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
listen [::]:80 ipv6only=on default_server;
# server_name domain.tld www.domain.tld;
server_name ec2...compute.amazonaws.com www.ec2...compute.amazonaws.com
root /home/ubuntu/appname/public;
try_files $uri/index.html $uri #app;
location #app {
proxy_pass http://ec2...us-east-2.compute.amazonaws.com;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
access_log /home/ubuntu/appname/log/nginx.access.log;
error_log /home/ubuntu/appname/log/nginx.error.log;
}
location ~* ^/assets/ {
# Per RFC2616 - 1 year maximum expiry
expires 1y;
add_header Cache-Control public;
# Some browsers still send conditional GET requests if there's a
# Last-Modified header or an ETag header even if they haven't
# reached the expiry date sent in the Expires header.
add_header Last-Modified "";
add_header ETag "";
break;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
I'm pretty new to this so I assume it's something basic, but I've been on this for hours and I'm not sure what to do. Also, if it helps, I'm using puma for my application server.
I am having a problem, I have set a nginx auth for my rails backend site which I am connecting with my rails frontend site using activeresource using self.user and self.password, everything works fine but when its loading images the backend site still asks for user name and login.
Any idea how to fix this?
Nginx config:
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
server_names_hash_bucket_size 64;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# Phusion Passenger config
##
# Uncomment it if you installed passenger or passenger-enterprise
##
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /usr/bin/ruby;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
nginx site config:
server {
listen 80;
listen [::]:80;
server_name servername.com;
passenger_enabled on;
passenger_friendly_error_pages on;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.1/wrappers/ruby;
rails_env production;
root /home/deploy/project_name/current/public/;
location / {
passenger_enabled on;
auth_basic "Authorization Required";
auth_basic_user_file /etc/nginx/.htpasswd;
}
I fixed this issue by adding an exception for image files.
location ~* \.(jpe?g|png|gif|ico)$ {
satisfy any;
auth_basic off;
}
I've set up an Ubuntu VPS and everything is up and running but I keep getting redirected to HTTPS which I don't want.
This is my config:
/etc/nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
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/wrappers/ruby-2.3.0/ruby;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
/etc/nginx/sites-available/default:
server {
listen 80 default_server;
server_name www.example.com localhost;
passenger_enabled on;
rails_env staging;
root /home/deploy/example.com/current/public;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
This is my output for lynx example.com:
Looking up example.com first
Looking up example.com
Making HTTP connection to example.com
Sending HTTP request.
HTTP request sent; waiting for response.
HTTP/1.1 301 Moved Permanently
Data transfer complete
HTTP/1.1 301 Moved Permanently
Using https://example.com/
Looking up example.com
Making HTTPS connection to example.com
Alert!: Unable to connect to remote host.
lynx: Can't access startfile http://example.com/
/var/log/nginx/access.log:
x.x.x.x - - [25/Apr/2016:09:25:30 +0000] "GET / HTTP/1.0" 301 0 "-" "Lynx/2.8.8rel.2 libwww-FM/2.14 SSL-MM/1.4.1 OpenSSL/1.0.2g"
Does anyone know why I keep getting redirected?
There is a configuration option in rails to force the use of SSL. Try setting
config.force_ssl = false
I am trying to get my Rails 4 working with SSL on a VPS with Ubuntu and NginX. I retrieved an SSL certificate from StartSSL.com and the installation on the server seems to have been successful.
However, I can't get my app to work with https. It only works with http at this moment.
When I try to access it in the browser through https I am getting this error:
2014/06/04 18:05:56 [error] 23306#0: *3 "/home/rails/public/index.html" is forbidden (13: Permission denied), client: 23.251.149.69, server: myapp.com, request: "GET / HTTP/1.0", host: "myapp.com"
This would be my NGINX configuration file in /etc/nginx/nginx.conf:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events { worker_connections 1024; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
server_names_hash_bucket_size 64;
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";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server {
listen 80;
server_name myapp.com;
rewrite ^ https://$server_name$request_uri? permanent;
}
server {
listen 443;
server_name myapp.com;
root /home/rails/public;
ssl on;
ssl_certificate /etc/ssl/myapp.com.crt;
ssl_certificate_key /etc/ssl/myapp.com.key;
}
}
What am I missing here and how can this be fixed?
I answered this over on DigitalOcean, but I noticed it here too.
You have an upstream set but no proxy_pass. I assume you're using something like Unicorn to serve the app? You probably need to adjust the server block listening on 443 to act as a reverse proxy for what ever is acting as an upstream server. Something like:
server {
listen 443;
server_name myapp.com;
root /home/rails/public;
index index.htm index.html;
ssl on;
ssl_certificate /etc/ssl/myapp.com.crt;
ssl_certificate_key /etc/ssl/myapp.com.key;
location / {
try_files $uri/index.html $uri.html $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
I'm hosting an API for a mobile app developed in Rails 4 on DigitalOcean using the One-Click Ubuntu 12.10 Installation droplet. Therefore nginx and Unicorn are preconfigured and running. When I try to open a file from Rails public folder on my web browser, it works.
But when I try to use any of the other paths defined in the routes.rb file I don't get any answer. My requests are loading until they time out. This changes when I type rails s -e production on the server – then everything works as expected. Therefore it must be a problem with nginx or something. What am I missing?
Here's my nginx sites-enabled/default file content (I don't know if that's the thing to look for, but maybe):
server {
listen 80;
root /home/rails/public;
server_name _;
index index.htm index.html;
location / {
try_files $uri/index.html $uri.html $uri #app;
}
#location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|mid$
location ~* ^.+\.(jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rt$
try_files $uri #app;
}
location #app {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://app_server;
}
}
UPDATE: Here is my nginx.conf file in case this helps:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events { worker_connections 1024; }
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
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";
gzip_types text/plain text/xml text/css text/comma-separated-values;
upstream app_server { server 127.0.0.1:8080 fail_timeout=0; }
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
UPDATE 2: And here my unicorn.conf file:
listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"
I had the same issue and running bundle install on the root of my app (on digitalocean droplet) resolved my issue. I think I have to build that into my capistrano script