Setting Up Uberjar with Nginx on Digital Ocean VPS - nginx

I used the instructions on the following link:
"Hosting Clojure Web Apps in 7 Easy Steps"
I know the uberjar works because i tested it both on my dev machine and the VPS.
It's just that Nginx doesn't seem to be able to find it.
I suspect that it has something to do with this site code:
# Web sockets
location /chsk {
proxy_pass http://backend/chsk;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
...but I don't know how to correct it...thanks for the help!
One other thing: on the "upstream backend" in the site file i tried both 127.0.0.1:3000 AND 0.0.0.0:3000 with no success.
Here's the default site config:
server {
# Replace this port with the right one for your requirements
listen [::]:80 ipv6only=off;
# Multiple hostnames separated by spaces. Replace these as well.
server_name clmitchell.net www.clmitchell.net main.clmitchell.net
books.clmitchell.net dna.clmitchell.net help.clmitchell.net
history.clmitchell.net svcs.clmitchell.net;
server_name_in_redirect off;
root /data/nginx/www/$host;
error_page 401 /error/401.shtml;
error_page 402 /error/402.shtml;
error_page 403 /error/403.shtml;
error_page 404 /error/404.shtml;
error_page 500 501 502 503 504 /error/500.shtml;
location ^~ /error/ {
internal;
root /data/nginx/www/www.clmitchell.net;
}
access_log /var/log/nginx/$host-access.log;
error_log /var/log/nginx/error.log;
index index.php index.html index.htm default.html default.htm;
# Support Clean (aka Search Engine Friendly) URLs
location / {
try_files $uri $uri/ /index.php?$args;
}
# serve static files directly
location ~* \.(jpg|jpeg|gif|css|png|js|ico)$ {
access_log off;
expires max;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location ~ \.scm$ {
include fastcgi_params;
fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need to
fastcgi_pass 127.0.0.1:9981;
}
location ~ /\.ht {
deny all;
}
}
I removed history.clmitchell.net from the list of server names.
Here's the current history site config:
upstream backend {
server 104.131.29.212:3000 fail_timeout=0;
}
server{
listen [::]:80 ipv6only=off;
server_name localhost history.clmitchell.net;
access_log /var/log/hist_access.log;
error_log /var/log/hist_error.log;
root /var//resources/public;
# Web sockets
location /chsk {
proxy_pass http://backend/chsk;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
# Static assets
location / {
try_files $uri #backend;
}
# The backend server
location #backend {
proxy_pass http://backend;
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_redirect off;
}
}
there was a duplicate "listen" directive on the history site config, which i removed...but for some reason I'm still getting the error: '
sudo nginx -t
nginx: [emerg] duplicate listen options for [::]:80 in /etc/nginx/sites-enabled/hist:6
nginx: configuration file /etc/nginx/nginx.conf test failed

Please try
proxy_pass http://backend;
And make sure you can access http://127.0.0.1:3000/chsk if your upstream is defined as below
upstream backend {
server 127.0.0.1:3000;
}
Or if we has only one backend server we can just use proxy_pass without upstream backend defined. e.g.
proxy_pass http://127.0.0.1:3000;

I learned a new lesson today: no two sites on a Nginx web server can have the same listen port!
I moved the new site to a new port and updated all the links...PROBLEM SOLVED!

Related

How to disable redirection of http to https (Nginx, CertBot)

Im trying to not redirect http to https....
I tried to research but found nothing...
BTW I DID BOTH THIS COMMANDS TO MAKE NEW FILE INSTEAD OF USING DEFAULT FILE ON SITES ENABLED:
sudo touch /etc/nginx/sites-available/imallbd
sudo nano /etc/nginx/sites-available/imallbd
then:
sudo ln -s /etc/nginx/sites-available/imallbd /etc/nginx/sites-enabled/imallbd
This is my sites-enabled file
server {
server_name imallbd.com;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
add_header X-Content-Type-Options "nosniff";
index index.html index.htm index.php;
charset utf-8;
# For API
location /api {
alias /var/www/imallbd/api/public;
try_files $uri $uri/ #api;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
}
}
location #api {
rewrite /api/(.*)$ /api/index.php?/$1 last;
}
# For FrontEnd -> GraphQL
location /{
proxy_pass http://localhost:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /admin{
proxy_pass http://localhost:3000/admin;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/imallbd.com/fullchain.pem; # managed >
ssl_certificate_key /etc/letsencrypt/live/imallbd.com/privkey.pem; # manage>
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = imallbd.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
listen 80;
server_name imallbd.com;
return 404; # managed by Certbot
}
pls help!!! btw when i go to my website it gives me 502 bad gateway... ik thats not the question im asking but if you can give me some help tips or the answer i would be so grateful :)
when i run:
sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
If you want more details or information i can give, just tell me on the comments!
THANKS IN ADVANCE!!!
first of all why do you want to remove the https redirect ?
Either way you can remove this part:
if ($host = imallbd.com) {
return 301 https://$host$request_uri;
} # managed by Certbot
This will remove the http -> https redirect.
Also, if you make any changes in nginx you need to restart te service.
assuming you are using a linux based os run:
systemctl restart nginx
The problem of your 502 error of nginx has to do with php.
The php process is not running, has crashed or nginx cannot communicate with it.
What kind of php instalation do you have ? php-fpm ? If that's the case run
systemctl restart php-fpm
If not. Let me know in the comments (not enough rep to say this in the comments)

Cannot GET /phpmyadmin after setting up phpmyadmin and reverse proxy in Nginx on Ubuntu 16.04

I followed Digital Ocean's tutorials on setting up Nginx, PHP, and phpmyadmin.
https://www.digitalocean.com/community/tutorials/how-to-install-and-secure-phpmyadmin-with-nginx-on-ubuntu-16-04
But I still can't access phpmyadmin with the address (my-ip-address/phpmyadmin) I set.
And I setup the reverse proxy for a node.js app listening on localhost:8010.
Here's the setup in the /etc/nginx/sites-available/default file:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name "my ip address";
location / {
proxy_pass http://localhost:8010/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
You're proxying all requests to http://localhost:8010 with this block:
location / {
proxy_pass http://localhost:8010/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
None of the requests can make it to phpmyadmin. Try commenting this block out or deleting it and it should work as you expect.
You need a block specifically for your location /phpmyadmin. Since you have set a rule to redirect to you fastCGI only if explicit extension is .php on the location ~ \.php$ { block, the /phpmyadmin location gets processed as a request to your proxy application. You have to add this:
location /phpmyadmin {
root /path/to/phpmyadmin;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
I added the following code to access the phpmyadmin:
location /phpmyadmin {
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
}

flask with web uwsgi and nginx not able to run index.html

I m very new in flask, try to run a web application with backend as flask. My project folder structure is
~myproject/
~myproject/app (flask api)
~myproject/web (index.html)
running using uwsgi and nginx
uwsgi.ini
[uwsgi]
vhost = true
socket = /tmp/flask.sock
venv = /flask_app/.env
chdir = /flask_app/app
module = app
callable = app
nginx.conf
upstream flask_server {
ip_hash;
server 0.0.0.0;
}
server {
listen 80;
server_tokens off;
server_name _;
root /flask_app/web;
index index.html index.htm index.nginx-debian.html;
charset utf-8;
client_max_body_size 75M;
location / {
#try_files $uri $uri/ =404;
include uwsgi_params;
uwsgi_pass unix:/tmp/flask.sock;
}
#location /static {
# alias /flask_app/static;
#}
location /flask/ {
proxy_redirect off;
proxy_pass http://flask_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
But index.html is not loading , it is only allowing api url (like: 127.0.0.1/login)?
This directive tells Nginx just to send everything matching that location block to the upstream server uwsgi_pass unix:/tmp/flask.sock
If you want to serve just index.html and pass everything else upstream then you could change it like this:
location / {
try_files $uri/index.html #upstream;
}
location #upstream {
include uwsgi_params;
uwsgi_pass unix:/tmp/flask.sock;
}

Nginx php site and node app on different port

I have installed nginx and configured php and mysql in my vps. My home page exist at /var/www/html. and it is working correctly when I access it form any computer.
Now I installed nodejs and set a simple hello world accoring to this link
my nginx defauls file is
server {
listen 80 default_server;
listen [::]:80 default_server;
location / {
root /var/www/html;
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
#try_files $uri $uri/ =404;
try_files $uri $uri/ = 404 $uri.html $uri/index.html #app;
#proxy_pass http://localhost:3000;
}
location #app {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
My problem is either my php files are served or node files are served, while I want, http://ipaddress:80 whould serve my php files, and http://ipaddress:3000 should serve my nodejs app.
I am using pm2 node module.
I am very-2 new to nginx.
Thanks
like this
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php;
location / {
# Redirect everything that isn't a real file to index.php
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php5-fpm.sock;
try_files $uri =404;
}
}
server {
listen 3000;
listen [::]:3000;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}

nginx try_files from proxy'd app, then nginx

I'm attempting to have nginx reverse proxy static files from an application if the application is serving them, else serve them itself. Currently, I have this configuration:
upstream app_server {
server unix:/tmp/gunicorn.sock fail_timeout=0;
}
server {
listen 8080;
server_name example.com;
access_log /var/log/nginx.access.log;
error_log /var/log/nginx.error.log;
keepalive_timeout 5;
location /static {
try_files $uri #proxy_to_app;
alias /path/to/__static;
sendfile off;
}
location / {
try_files $uri #proxy_to_app;
}
location #proxy_to_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;
}
}
This works if the files don't exist in /path/to/__static; it sends the request to the application server. However, if the files also exist in /path/to/__static, nginx serves the files itself.
Reversing the try_files line (try_files #proxy_to_app $uri) fails in both cases. If the client requests /static/css/test.css, the application receives a request for /css/test.css, and it never seems to try /path/to/__static even though the application returns a 404.
Updated to include full configuration.
location /static/ {
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;
proxy_intercept_errors on;
error_page 404 =200 /local$uri;
}
location /local/static/ {
internal;
alias /path/to/__static/;
}

Resources