Nginx not accepting requests after configuring SSL with Let's Encrypt
Ritwik Das
rtwk.das at gmail.com
Mon Oct 9 09:20:48 UTC 2017
Hello,
I used to run multiple nodejs apps with Nginx on a Ubuntu Server.
Those apps used to receive Ajax requests from another domain. Nginx used to
receive those requests on different ports and then nodejs used to spit
output. After I configured SSL using Let's Encrypt on both of the domains,
apps are running (I can see them running using forever list), but they are
not accepting those requests (Error connection closed). I will be thankful
you experts may help me to fix the issue.
I am attaching Nginx configuration file, nodejs code and client side Ajax
code. I can't send files as is because email attachment is getting blocked,
so I added a .txt extension after original file names.
Regards
Ritwik Das
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20171009/dd1657c0/attachment.html>
-------------- next part --------------
server {
listen 80;
server_name w3rpractice.com;
location /home/w3r/editor/server.js {
proxy_pass http://0.0.0.0/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor.js {
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,Content-Range,Range';
#
# 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,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
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,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
proxy_pass http://0.0.0.0:9117/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_pyhon.js {
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,Content-Range,Range';
#
# 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,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
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,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
}
proxy_pass http://0.0.0.0:9118/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_php.js {
proxy_pass http://0.0.0.0:9130/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_ruby.js {
proxy_pass http://0.0.0.0:9119/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_java.js {
proxy_pass http://0.0.0.0:9121/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_cs.js {
proxy_pass http://0.0.0.0:9122/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_swift.js {
proxy_pass http://0.0.0.0:9124/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_cpp.js {
proxy_pass http://0.0.0.0:9125/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_mongo.js {
proxy_pass http://0.0.0.0:11000/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_mysql.js {
proxy_pass http://0.0.0.0:11100/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /home/w3r/editor/editor_mysql2.js {
proxy_pass http://0.0.0.0:11200/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 43200000;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/w3rpractice.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/w3rpractice.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
if ($scheme != "https") {
return 301 https://$host$request_uri;
} # managed by Certbot
ssl_dhparam /etc/ssl/certs/dhparam.pem;
}
-------------- next part --------------
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
var fs = require('fs');
var path = require('path');
var random_port = require('random-port');
var mkdirp = require('mkdirp');
var chmod = require('chmod');
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, '/')));
app.listen(9121,function(){
console.log("Hi how r u doing?");
});
app.all('/', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
});
app.post('/',function(req,res){
var topic = req.body.topic;
var user_code=req.body.code_sent;
user_code = user_code.replace(/w3rplus/g,"+");
user_code = user_code.replace(/w3rminus/g,"&");
var fname = req.body.fname;
var fname_java = fname + ".java";
var uuid = require('node-uuid');
var newDir = uuid.v1();
mkdirp('/home/students/'+ newDir, function (err) {
if (err) console.error(err)
else console.log('pow!')
});
// chmod('home/students/'+ newDir,777');
var dir = "/home/students/" + newDir;
fs.writeFile(dir + "/" + fname_java, user_code, function (err) {
if (err) throw err;
console.log('It\'s saved!');
});
var sys = require('sys');
random_port({from:3800, range:99}, function(port) {
console.log(port);
var exec = require('child_process').exec;
var term = "timeout -s KILL 40 web-term -c /home/students/ -H 45.55.241.251 -p " + port + " -s \" cd " + newDir + " && javac " + fname_java + " && timeout -s KILL 30 java " + fname + "\"";
var child = exec(term);
if(child){
//res.end("http://162.243.243.22:" + port);
res.end(port + "W3R" + fname);
}
});
});
-------------- next part --------------
<style type="text/css" media="screen">
#editor {
position: relative;
width: 550px;
height: 300px;
font-size: 120%
}
.console-log-div {
width: 95% !important;
background-color: #efefef;
}
#log {
margin: 10px 0px;
display: block;
white-space: pre;
font-family: monospace;
}
#log:before {
content: "log javascript:";
font-style: italic;
color: #555;
}
#w3r_op iframe {
margin-top: 20px;
height: 400px;
}
#editor {
width: 100%
}
#terminal {
font-size: 17px;
background-color: #32c1e5;
color:#FFFF00
}
</style>
<script src="https://www.w3resource.com/new_editor/ace-builds/src-noconflict/ace.js" type="text/javascript" charset="utf-8"></script>
<pre id="editor" contenteditable>
<?php echo $code; ?>
</pre>
<button type="button" class="btn btn-success" id="run">RUN</button>
<script src="https://www.w3resource.com/new_editor/seamless-polyfill/seamless.min.js"></script>
<script type="text/javascript">
seamless.parent({
'allowed_domains': ['https://w3rpractice.com'],
'target_domain': '*'
});
</script>
<div id="w3r_op"></div>
<script>
var editor = ace.edit("editor");
editor.setTheme("ace/theme/eclipse");
editor.getSession().setMode("ace/mode/python");
</script>
<script>
function user_code(){
document.getElementById("w3r_op").innerHTML='';
var xhr, code, str, topic;
topic = "java";
code = editor.getValue();
code = code.trim();
code = code.replace(/[+]/g, 'w3rplus');
code = code.replace(/[&]/g, 'w3rminus');
var new_code = code.match("public(.*){");
var className = new_code[0].split(/(\s+)/);
var fName = className[4];
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE 8 and older
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
//console.log(Finalcode);
var data = "code_sent=" + code + "&fname=" + fName + "&topic=" + topic;
xhr.open("POST", "https://w3rpractice.com:9121", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);
xhr.onreadystatechange = display_data;
function display_data() {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
var code_str = xhr.responseText;
var splits = code_str.split('W3R');
var port = splits[0];
console.log(port);
var op = document.getElementById('w3r_op');
var iframe = document.createElement('iframe');
iframe.setAttribute('frameborder','0');
iframe.setAttribute('id','result');
iframe.setAttribute('src', "https://w3rpractice.com:"+port);
op.appendChild(iframe);
} else {
console.log('There was a problem with the request.');
}
}
}
}
/*function user_code(txt) {
console.log("dsfds");
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://w3rpractice.com', true);
xhr.onload = function(e) {
if (this.status == 200) {
console.log(this.responseText);
}
};
xhr.send(txt);
}
user_code('test string');*/
var el = document.getElementById("run");
el.addEventListener("click", user_code, false);
</script>
More information about the nginx
mailing list