I am having an issue with my NGINX+Passenger+Rails setup. One requests takes forever to run and is not canceled. I am not sure what is the reason, I have no errors in my error.log file.
Surprisingly if I switch to Puma as a webserver everything is working fine.
The code that causes the issue in my application is marked with a comment:
def build_redirect_url
raise ArgumentError.new("not implemented for provider") if checkout_settings[:package_provider] != "expedia_click_and_mix"
opts = {
:hotels => [hotel],
:from_date => from_date,
:to_date => to_date,
:from_airport_code => from_airport.code,
:to_airport_code => to_airport.code,
:number_of_adults => number_of_adults.to_i,
:cache => false,
:piid => checkout_settings[:unique_identifier]
}
# the below line never finishes. takes like 30 seconds
searcher = PackageKraken::ListKraken::HotelGrouper.new(opts)
details_url = searcher.search.first.details_url
filter_id = search_filter_setting.id
build_filter_redirect_url(filter_id, "expedia_click_and_mix", hotel.id, details_url)
end
We never make it past the searcher = line. It seems the process dies before. So what I did is check my nginx log file for issues, but I only have this. Hoever it seems to have a 499 error code.
This is what I have in my log file:
79.236.111.56 - - [02/Nov/2016:14:28:30 +0100] "GET /packages/package_redirect_url?checkout_settings=%7B%22package_identifier%22%3A%22v5-8a5c783c4b614f2d8018117d4c7fa1f5-8-8-1%22
%2C%22package_provider%22%3A%22expedia_click_and_mix%22%7D&from_airport_id=b2ccff00-2186-482e-a74f-6892c8fd7f77&from_date=2016-11-09+01%3A00%3A00+%2B0100&hotel_id=14245&number_of
_adults=1&to_airport_id=aabc5cd4-36e6-45c9-b027-e0ed4b209414&to_date=2016-11-15+01%3A00%3A00+%2B0100 HTTP/1.1" 499 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWeb
Kit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36"
Here is my site config for the website:
server {
listen 80;
server_name tripl.de www.tripl.de;
return 301 https://$host$request_uri;
}
server {
listen 443;
server_name tripl.de;
ssl on;
ssl_certificate /etc/nginx/ssl/wildcard-cert.crt;
ssl_certificate_key /etc/nginx/ssl/wildcard-cert.key;
return 301 https://www.tripl.de$request_uri;
}
# Production server
server {
proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
listen 443;
server_name www.tripl.de;
ssl on;
ssl_certificate /etc/nginx/ssl/wildcard-cert.crt;
ssl_certificate_key /etc/nginx/ssl/wildcard-cert.key;
client_max_body_size 4G;
keepalive_timeout 60;
passenger_enabled on;
root /home/deployer/app_production/current/public;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
}
}
And this is what I have in my passenger.conf:
passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deployer/.rbenv/shims/ruby;
Any ideas what could be wrong?
Thanks
The problem was me using Celluloid in some area. Apparently passenger does not like spawning new threads.
Related
Below is my AWS NGINX config file. I am using rich text with active storage in my new rails app after upload when i am trying to open a file it gives me 404 error in production where in development it works really fine.
files:
/etc/nginx/conf.d/proxy.conf:
mode: "000755"
owner: root
group: root
content: |
upstream backend {
server unix:///var/run/puma/my_app.sock;
}
log_format logd '$msec"$uri"'
'$status"$request_time"$upstream_response_time"'
'$http_x_forwarded_for';
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
return 301 https://$host$request_uri;
}
server {
listen 443;
charset UTF-8;
server_name _ localhost; # need to listen to localhost for worker tier
root /var/app/current/public;
# try_files $uri/index.html $uri /deploy/$uri /deploy/$uri.html /deploy/$uri.js #puma;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/nginx/access.log main;
access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour logd;
# 413 Request Entity Too Large
client_max_body_size 50M;
large_client_header_buffers 8 32k;
location / {
try_files $uri /deploy/$uri /deploy/$uri.html /deploy/$uri.js #puma;
}
location #puma{
proxy_pass http://backend;
# proxy_pass http://backend; # match the name of upstream directive which is defined above
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 https;
# prevents 502 bad gateway error
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_redirect off;
#break;
}
location /assets {
alias /var/app/current/public/assets;
allow all;
}
location ~ \.(png|jpg|jpeg|gif|ico|html|woff|woff2|ttf|svg|eot|otf|pdf)$ {
expires max;
access_log off;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc|pdf)$ {
access_log off;
add_header Cache-Control "max-age=2592000";
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
Whenever i open Link with file extension it shows me 404 error but if i open the same link without file extension it works Link without file extension. Not sure what i am doing wrong please help
Try disabling the below blocks in your Nginx, this might fix it
location ~ \.(png|jpg|jpeg|gif|ico|html|woff|woff2|ttf|svg|eot|otf|pdf)$ {
gzip_static on;
gzip on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_vary on;
gzip_proxied any;
expires max;
access_log off;
add_header Cache-Control public;
add_header Access-Control-Allow-Origin *;
}
location ~* \.(?:jpg|jpeg|gif|png|ico|cur|gz|svg|mp4|ogg|ogv|webm|htc|pdf)$ {
access_log off;
add_header Cache-Control "max-age=2592000";
}
I am working on a Ruby on Rails project on an Ubuntu server. Whenever I try and access the app, I am always greeted with:
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
Here is my code from the "sites_enabled" directory:
pstream unicorn_site {
server unix:/tmp/unicorn.site.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name http://[ip_address];
root /data/site/current/public;
try_files $uri/index.html $uri #unicorn_site;
location #unicorn_site {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://unicorn_site;
# limit_req zone=one;
access_log /var/log/nginx/site.access.log;
error_log /var/log/nginx/site.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
I am not sure what the problem is as the app seems to have everything it need to be working correctly. I can provide any part of the code anyone needs. Help is much appreciated.
EDIT: I am also always getting this error when I attempt to load the page:
2018/06/15 15:39:10 [warn] 15280#0: server name "http://[ip_address]" has suspicious symbols in /etc/nginx/sites-enabled/site:14
EDIT 2: Here is nginx.conf
user www-data;
worker_processes 4;
pid /run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
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;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Move 'try_files $uri/index.html $uri #unicorn_site;' under location /
location / {
try_files $uri/index.html $uri #unicorn_site;
}
The server_name http://[ip_address]; looks wrong. try server_name example.com
Have you configured ruby_passenger line ?
Follow this carefully and your deployment should be successful
Choose your ubuntu version there and proceed
https://gorails.com/deploy/ubuntu/16.04
Running a ruby on rails application but have wordpress integrated under the /blog on the domain.
The problem I'm having is that none of the asset files are served correctly under the /blog url.
The wordpress php files are routed correctly and work. The issue is that I'm trying to route the wordpress theme and plugin files, namely css and js files to the /blog folder. However I'm getting 404 for the static files served under /blog so I think I have a misconfiguration in my nginx conf file.
Current nginx configuration:
server {
listen 3000;
server_name myapp.com;
access_log off;
location /blog {
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
access_log off;
add_header Cache-Control public;
root /var/www/wordpress/current/blog;
break;
}
root /var/www/wordpress/current/blog;
index index.php index.html index.htm;
rewrite ^/blog/(.*)$ /blog/$1 break;
try_files $uri $uri/ /index.php?$args;
}
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
root /u/apps/myapp/current/public;
expires max;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
access_log off;
add_header Cache-Control public;
root /u/apps/myapp/current/public;
break;
}
client_max_body_size 50M;
root /u/apps/myapp/current/public;
access_log off;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.3#myapp/wrappers/ruby;
passenger_enabled on;
passenger_max_request_queue_size 200;
rails_env production;
if ($host != 'myapp.com') {
rewrite ^/(.*)$ http://myapp.com/$1 permanent;
}
location ~* ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
error_page 500 504 /500.html;
location = /500.html {
root /u/apps/myapp/current/public;
}
error_page 502 503 /503.html;
location = /503.html {
root /u/apps/myapp/current/public;
}
error_page 404 /404.html;
location = /50x.html {
root html;
}
location ~ .*\.php$ {
root /var/www/wordpress/current;
#fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS 'on';
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
add_header Access-Control-Allow-Origin *;
}
}
There is a difference between root and alias, I think you're looking for alias in this situation.
When you use root nginx appends the URI to the path, so using root /var/www/wordpress/current/blog; will cause this to be the root directory for the request, which means navigating to /blog/css/style.css will cause nginx to look for /var/www/wordpress/current/blog/blog/css/style.css.
If you use an alias instead, then nginx will map the uri to the directory:
alias /var/www/wordpress/current/blog;
When you navigate to /blog/css/style.css nginx will remove the prefix and serve the file from /var/www/wordpress/current/blog/css/style.css, it seems you're attempting to do this with a rewrite however your rewrite is rewriting the request to the same uri.
In the situation the URL doesn't work your error_log should be your friend, it'll tell you exactly where it's looking:
2017/06/15 13:04:19 [error] 43391#0: *1786 open()
"/var/www/wordpress/current/blog/blog/css/styles.css" failed
(2: No such file or directory), client: 127.0.0.1, server: myapp.com,
request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"
Changing this to alias throws an error for me (because I don't have your directory structure) but it shows how the location changes:
2017/06/15 13:06:12 [error] 43582#0: *1787 open()
"/var/www/wordpress/current/blog/css/styles.css" failed
(2: No such file or directory), client: 127.0.0.1, server: myapp.com,
request: "GET /blog/css/styles.css HTTP/1.1", host: "myapp.com:3000"
You also don't have a lot of duplicate directives, you only need to define the them once as they are inherited by children, this can clean up your configuration file a lot making it easier to switch things around in the future:
server {
client_max_body_size 50M;
listen 3000;
server_name myapp.com;
access_log off;
root /u/apps/myapp/current/public; # default root, use this unless specified otherwise
error_page 500 504 /500.html;
error_page 502 503 /503.html;
error_page 404 /404.html;
location /blog {
alias /var/www/wordpress/current/blog; # overwrite the default root for this entire block
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
add_header Cache-Control public;
break;
}
}
location ~* ^.+\.(jpg|jpeg|gif|png|css|bmp|js|ico|swf)$ {
expires max;
}
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires max;
add_header Cache-Control public;
break;
}
location ~* "^.*?\.(eot)|(ttf)|(woff)$" {
add_header Access-Control-Allow-Origin *;
}
if (-f $request_filename.html) {
rewrite (.*) $1.html break;
}
if ($host != 'myapp.com') {
rewrite ^/(.*)$ http://myapp.com/$1 permanent;
}
location ~* ^/assets/ {
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
location = /50x.html {
root html; # overwrite the default root for this
}
location ~ .*\.php$ {
root /var/www/wordpress/current; # overwrite the default root, because this doesn't have /blog on the end it will properly map to /var/www/wordpress/current/blog when /blog is accessed
#fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param HTTPS 'on';
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
# this block is only processed if nothing else matches
location / {
passenger_ruby /home/deploy/.rvm/gems/ruby-2.3.3#myapp/wrappers/ruby;
passenger_enabled on;
passenger_max_request_queue_size 200;
rails_env production;
}
}
I have the application written in Rails and Ember frontend for it. It is accessible by nginx server. Here is configuration for Rails part:
upstream app_project_app {
server unix:///tmp/project.sock fail_timeout=0;
}
And here is configuration for ember part:
server {
listen 80;
server_name project.demo.domain.pl;
root /home/lunar/apps/project-ember/current;
try_files /system/maintenance.html $uri/index.html $uri.html $uri #app;
access_log /var/log/nginx/project_app_access.log;
error_log /var/log/nginx/project_app_error.log;
keepalive_timeout 5;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_connect_timeout 60;
if ($request_method !~ ^(GET|HEAD|PUT|POST|DELETE|OPTIONS)$ ){
return 405;
}
location ~ ^/assets/ {
expires max;
add_header Cache-Control public;
add_header ETag "";
break;
}
location = /favicon.ico {
expires max;
add_header Cache-Control public;
}
location / {
try_files $uri/index.html $uri.html $uri #app;
error_page 404 /404.html;
error_page 422 /422.html;
error_page 500 502 503 504 /500.html;
error_page 403 /403.html;
}
location #app {
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://app_project_app;
}
}
Now the application grown and has websockets server (using faye). And the client can't connect to the server:
WebSocket connection to 'ws://project.demo.domain.pl/faye' failed: Error during WebSocket handshake: Unexpected response code: 400
I've read, that I need to enable SSL for this handshake. How can I do this in nginx? I also read, that I don't need to use https and I can use SSL only for websockets, is it true? And if yes, how should look configuration for nginx in this case?
For websocket support you need add the following directives in your #app location block
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgradeā;
Read more here
Using the websocket-rails gem, I'm able to successfully get a websocket connection straight through puma in development, however, when deployed to production and attempting to access the websocket through nginx (passing off to puma) I have a couple of errors: one in the nginx error log:
[info] 14340#0: *7 upstream timed out (110: Connection
timed out) while proxying upgraded connection, client: 123.45.67.89, server:
foo.com, request: "GET /websocket HTTP/1.1", upstream:
"http://unix:///opt/oneconnect/shared/tmp/sockets/puma.sock:/websocket", host:
"foo.com"
... and one on the javascript console:
WebSocket connection to 'ws://foo.com/websocket' failed: Error during WebSocket handshake: Unexpected response code: 301
I found that nginx (the version I'm using is 1.4.6) is capable of websocket use but requires special configuration, which I've already attemped (getting the errors above). Here's my nginx.conf:
upstream oneconnect {
server unix:///opt/oneconnect/shared/tmp/sockets/puma.sock;
}
server {
listen 80;
listen 443 ssl;
#ssl on;
ssl_certificate /etc/ssl/foo.com.crt;
ssl_certificate_key /etc/ssl/foo.com.key;
root /opt/oneconnect/current/public;
try_files $uri #oneconnect;
access_log /opt/oneconnect/current/log/nginx.access.log;
error_log /opt/oneconnect/current/log/nginx.error.log info;
server_name foo.com;
location ~ ^/(assets)/ {
root /opt/oneconnect/current/public;
gzip_static on;
expires max;
add_header Cache-Control public;
}
location /websocket/ {
proxy_pass http://oneconnect;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location #oneconnect {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass http://oneconnect;
}
}
I'm assuming that I'm missing something simple, but I'm stumped at this point and have Googled until my eyes started bleeding. If anyone could help it would be much appreciated, or maybe just point me to how to debug these connections (it seems hard to get debug info from a ws connection). Thanks for your time.
Assuming u have already initializer for eventmachine
config/initializers/eventmachine.rb
Thread.new { EventMachine.run } unless EventMachine.reactor_running? && EventMachine.reactor_thread.alive?
nginx site conf:
upstream puma_project_production {
server unix:/var/www/project/shared/tmp/sockets/puma.sock fail_timeout=0;
}
server {
listen 80;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 #503;
server_name localhost project.local;
root /var/www/project/current/public;
try_files $uri/index.html $uri #puma_project_production;
location #puma_project_production {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma_project_production;
# limit_req zone=one;
access_log /var/www/project/shared/log/nginx.access.log;
error_log /var/www/project/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location #503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
location /websocket {
proxy_pass http://puma_project_production;
proxy_redirect off;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ \.(php|rb)$ {
return 405;
}
}