Rails6 : Multiple applications with NGINX passenger not serving POST requests from sub-URIs - ruby-on-rails-6

I want to run two Rails6 applications on the same server at two sub-URIs using NGINX and Passenger. The root is a static index.html.
The index page loads correctly from the root. So do the root views of the Rails applications when the sub-URIs are accessed. However, subsequent form submits throw 404.
Both the applications run fine when I run them on root (/) instead of the static index page.
Configuration file is sites-enabled folder has --
server {
listen 80;
server_name _;
passenger_enabled on;
passenger_ruby /usr/bin/ruby;
passenger_app_env development;
location / {
root /var/www/html;
}
location ~ ^/rbf(/.*|$) {
alias /var/www/rails6-bootstrap-flatpickr/public$1;
passenger_base_uri /rbf;
passenger_app_root /var/www/rails6-bootstrap-flatpickr;
passenger_document_root /var/www/rails6-bootstrap-flatpickr/public;
}
location ~ ^/cdb(/.*|$) {
alias /var/www/dashboard/public$1;
passenger_base_uri /cdb;
passenger_app_root /var/www/dashboard;
passenger_document_root /var/www/dashboard/public;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
The NGINX access log has the following line (I have replaced my ip address with aaa.bbb.ccc.ddd)
[19/Nov/2020:12:00:59 +0000] "POST /display_router HTTP/1.1" 404 209 "http://aaa.bbb.ccc.ddd/cdb" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36"

In development I had each app running on its own Rails server.
The form definitions were:
<%= form_with(url: "/process_dates") do %>
and
<%= form_with(url: "/display_router") do %>
The forms were being posted to root folder /process_dates and /display_router respectively.
But on the cloud server, each application is in its own folder which is the rails root.
So I just changed the form definitions on the cloud server to
<%= form_with(url: "/rbf/process_dates") do %>
and
<%= form_with(url: "/cdb/display_router") do %>
and it worked.

Related

nginx 404 not found but I get hits in my access.log. Then why don't I see my site?

This is tricky because it doesn't make sense. On the same server I have other sites that work just fine. The configuration file for each is the same. I created this new site, example.com and this is the configuration
server {
listen 80;
listen [::]:80;
server_name example.com;
access_log /var/log/nginx/example.com/access.log;
error_log /var/log/nginx/example.com/error.log;
root /var/www/example.com/default/web;
}
I left the minimum in this file.
I linked it, but when I go to the browser I get 404 Not Found.
So in the root, I created a file 1.txt just to check it on the browser. I still got 404.
I looked in the /var/log/nginx/example.com/access.log and /var/log/nginx/example.com/error.log and there was nothing. Then I looked at the general log, /var/log/nginx/access.log and I was getting this:
[21/Feb/2020:10:36:21 +0000] "GET /1.txt HTTP/1.1" 404 162 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:73.0) Gecko/20100101 Firefox/73.0"
So the request gets to the server but it's not reaching the root?
I made sure all permissions are directories 775 and files 774, users and groups are correct.
I reloaded and also restarted nginx.
I am out of ideas, I don't know what else is wrong. Can someone give me a hint?
UPDATE:
I have the ip of the server and the site in my hosts file because I don't have it on DNS yet. When I do example.com it's 404. When I look for https://example.com I see the Warning: Potential Security Risk Ahead. I proceed and then I see one of my other sites on the server. So there is a reason why the request is not going to the example.com but to another, even though I see it when I look for nginx -T | grep server_name.

Server receives POST request twice from Nginx

We have a nginx server acting as a reverse proxy between the client and server.
Whenever the server returns a 500 we actually see that the request is being sent to the server twice from the nginx logs:
173.38.209.10 - - [26/Jan/2018:15:15:36 +0000] "POST /api/customer/add HTTP/1.1" 500 115 "http://apiwebsite.com" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
173.38.209.10 - - [26/Jan/2018:15:15:36 +0000] "POST /api/customer/add HTTP/1.1" 500 157 "http://apiwebsite.com" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36"
This API is only called twice if the first response is a 500.
If I bypass the nginx proxy and call the server directly, then it's only called once.
What's more strange is after further testing we found out this only happens in our corporate network. If i use my home network to connect to the proxy, then there's no retry even in case of a 500 response.
Anway, here's my nginx configuration:
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass "http://127.0.0.1:3000";
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /api/customer/ {
proxy_pass "http://127.0.0.1:8080/";
}
}
Is there anything suspicious which is causing this behaviour?
Thanks

nginx on docker doesn't work with location URL

I am running nginx in docker to act as a reverse proxy for multiple applications. for e.g.,
http://localhost/eureka/ will show http://registry:8761
http://localhost/zipkin/ will show http://zipkin:9411
I started with following nginx conf,
http {
server {
location /eureka/ {
proxy_pass http://registry:9761;
}
}
}
The above configuration is not working and nginx throwing error as,
proxy | 172.20.0.1 - - [24/Mar/2017:10:46:28 +0000] "GET /eureka/ HTTP/1.1" 404 0 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.110 Safari/537.36"
But the below configuration works for http://localhost/ showing eureka page.
http {
server {
location / {
proxy_pass http://registry:9761;
}
}
}
What I am missing? As per nginx proxy_pass it should work, but its not.
The proxy_pass directive can optionally modify the URI before it is passed upstream. To remove the /eureka/ prefix simply append the URI / to the proxy_pass statement.
For example:
location /eureka/ {
proxy_pass http://registry:9761/;
}
The URI /eureka/foo will be mapped to http://registry:9761/foo. See this document for more.
Of course, this is only half of the problem. In many cases, the upstream application must access its resources using the correct prefix or a path-relative URI. Many applications cannot be forced into a subdirectory.

Complicated Wordpress scenario trying to get https up and running

I honestly have no idea about a more tentative and suggestive title, so... sorry to all moderators in advance.
Ok, so I configured properly Let's Encrypt to my website with Nginx. Once I finished, I see the website without CSS, JS, etc. in a typical blocked:mixed:content (BMC) problem. BMC is easily fixable, you check wordpress URL configuration plus one or two more stuffs.
So here's where things start to get complicated. Only after I successfully logged into my wp-admin, Wordpress shows me a Sorry, you are not allowed to access this page., which in the back it's a 403 error. At that time, error.log wont show anything, however access.log show this:
160.160.160.160 - - [26/Dec/2016:11:43:59 -0600] "GET /wp-admin/ HTTP/2.0" 403 1826 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
160.160.160.160 - - [26/Dec/2016:11:43:59 -0600] "GET /favicon.ico HTTP/2.0" 200 159 "https://www.paradisearenasblancas.com/wp-admin/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36"
(Fake IPs BTW)
So far everything is a mere information and I can't see a sole useful information about the error in question.
I have configured my Wordpress installation with the old http URL, so after I can't enter the dashboard, I can edit those URLs directly with MySQL or manually adding it in wp-config. The problem with that is that it gives me the net::ERR_TOO_MANY_REDIRECTS error in browsers. So it's kind a bottleneck scenario, and I have no idea what to do.
I can't find an error in my website.conf file:
server {
listen 80;
listen [::]:80;
server_name www.mywebsite.com;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
include snippets/ssl-www.mywebsite.com.conf;
include snippets/ssl-params.conf;
server_name www.mywebsite.com;
root /usr/share/nginx/html/mywebsite.com/;
index index.php index.html;
error_log /var/log/nginx/error-paradise.log;
access_log /var/log/nginx/access-paradise.log;
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
# Wide-open CORS config for nginx
add_header 'Access-Control-Allow-Origin' 'https://www.mywebsite.com';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Headers' 'Content-Type,Accept';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
}
location ~ /.well-known {
allow all;
}
}
Another thing I did was to move all plugins from plugins directory and nothing, everything still the same.
I have no idea what else to do. Any help will be much appreciated.
EDIT:
It seems to be the theme I'm using. I've tried to move its directory and when Wordpress changes to the next default theme, everything is working fine. If I copy again the theme's directory to themes/, the same problem will start. Since I have lots of blocked:mixed:content errors, I'm searching within theme directory using grep -rnw '/usr/share/nginx/html/website.com/wp-content/themes/themename/' -e 'http'and found a huge amount of lines containing http. I know there's a lot of plugins that fixes this, but since I'm getting error 403 once I'm logged in into wp-admin, how can I replace all that http into https?
TO enable SSL for wordpress, you can do any of the two below
Edit both your SITE and HOME URL settings in the Settings -> General section of your wordpress dashboard, changing the http to https.
Edit your wp-config.php and define both the WP_SITEURL and WP_HOME
define( 'WP_SITEURL', 'https://example.com' ); and define( 'WP_HOME', 'https://example.com' );
You should also add this to your wp-config.php force ssl for wp-admin
define('FORCE_SSL_ADMIN', true);

nginx returns 405 (Method Not Allowed) for PUT or DELETE

I am using nginx to serve static pages but to pass requests to an API on to a Tornado application, which I would like to handle GET, POST, PUT and DELETE requests.
GET and POST requests are fine, but PUT and DELETE requests are rejected with "405: Method Not Allowed"
This question asks much the same: How do I allow a PUT file request on Nginx server? but the answer doesn't solve my issue, which makes me think it's related to my use of proxy_pass in my setup.
Here's my nginx server config:
upstream TornadoAPI {
server 127.0.0.1:8000;
}
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name localhost;
location /<<static url>>/ {
root /var/www;
index index.html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.html;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
location /<<API url>>/ {
dav_methods PUT DELETE;
dav_access all:r;
proxy_pass http://TornadoAPI/api/;
}
}
I have attempted to use the HttpDavModule directives (though I don't think my application qualifies as HttpDav - I have no intention on letting users write files) without luck. I have confirmed the module's presence by inspecting nginx -V.
Here's an example output from the nginx access.log:
<<IP address>> - - [06/Mar/2014:16:29:57 +0000] "PUT /<<API url>>/<<resource>> HTTP/1.1" 405 87 "<<ngix server root url>>" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36"
Can someone suggest what else I can do to accept the PUT and DELETE methods?
You can add this sentence in your configuration file
dav_methods PUT DELETE MKCOL COPY MOVE;
Specific detailed reference nginx document http://nginx.org/en/docs/http/ngx_http_dav_module.html

Resources