I have tried all the solution on SO but no success. I want to use Nginx as a "Node.js" app reverse proxy. With my current configurations, I was able to make it work when connecting to it through the server IP but not when using its domain name.My configuration details pastebin.com/gMqpmDwj
http://Ipaddress:3000 works but http://example.com doesn't.
Here is the configuration of my Nginx proxy, stored in /etc/Nginx/conf.d/domain.conf.
server {
listen 80;
server_name domain_name;
location / {
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://ipaddress:3000;
}
}
But when I try to access it works fine on ip:port but when on domain:port or without port it doesn't
Try this configuration:
/etc/nginx/nginx.conf
user nobody;
worker_processes 1;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
multi_accept on;
}
http {
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 15;
types_hash_max_size 2048;
client_max_body_size 8M;
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log off;
error_log /var/log/nginx/error.log crit;
gzip on;
gzip_min_length 100;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/cloudflare.inc;
include /etc/nginx/conf.d/*.conf;
}
/etc/nginx/conf.d/domain.conf
upstream nodejs_app {
server <ipaddress>:3000;
keepalive 8;
}
server {
listen 80;
listen [::]:80;
server_name <domain_name>;
location / {
# websocket support
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://nodejs_app/;
proxy_redirect off;
}
}
I solved my issue after following this link.I had multiple configuration files active that was causing problem.
How to Configure Nginx Reverse Proxy for Nodejs on Centos
Related
i have installed two odoo instances on my VPS, now i'm trying to configure nginx to use both domains with their ports, i am a beginner in nginx, i tried searching in the web but nothing is clear enough, i followed a guide on how to install odoo but it only shows nginx configuration for a single domain.
this is the config i'm currently using :
upstream odooserver {
server 127.0.0.1:8050;
}
server {
listen 80;
server_name www.domain.com;
access_log /var/log/nginx/odoo_access.log;
error_log /var/log/nginx/odooe_error.log;
proxy_read_timeout 720s;
proxy_connect_timeout 720s;
proxy_send_timeout 720s;
proxy_set_header X-Forwarded-Host $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;
location / {
proxy_redirect off;
proxy_pass http://odooserver;
}
location ~* /web/static/ {
proxy_cache_valid 200 90m;
proxy_buffering on;
expires 864000;
proxy_pass http://odooserver;
}
gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript;
gzip on;
}
it's working now after editing the config file like the others suggested, and it turned out that there was something wrong with the DNS settings.
I take Nginx as my reverse proxy to forward the requests to websites and api. But if I call the api many times, the website will stop at "Redirecting" page and I have to click the url manually.
Here is the screen
Here is my nginx confiuration(I hidden the ssl congifuration):
user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;
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;
gzip on;
server {
listen 80;
server_name alpha.hunghingprinting.com;
rewrite ^(.*) https://$host$1 permanent;
}
server {
listen 443;
# set proper server name after domain set
server_name alpha.hunghingprinting.com;
# Add Headers for odoo proxy mode
proxy_set_header X-Forwarded-Host $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;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-XSS-Protection "1; mode=block";
proxy_set_header X-Client-IP $remote_addr;
proxy_set_header HTTP_X_FORWARDED_HOST $remote_addr;
# SSL parameters
ssl on;
ssl_prefer_server_ciphers on;
# odoo log files
access_log /var/log/nginx/odoo14-access.log;
error_log /var/log/nginx/odoo14-error.log;
# increase proxy buffer size
proxy_buffers 16 64k;
proxy_buffer_size 128k;
proxy_read_timeout 900s;
proxy_connect_timeout 900s;
proxy_send_timeout 900s;
# force timeouts if the backend dies
proxy_next_upstream error timeout invalid_header http_500 http_502
http_503;
types {
text/less less;
text/scss scss;
}
# enable data compression
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/css text/less text/plain text/xml application/xml application/json application/javascript application/pdf image/jpeg image/png;
gzip_vary on;
client_header_buffer_size 4k;
large_client_header_buffers 4 64k;
client_max_body_size 0;
location / {
proxy_pass http://127.0.0.1:8069;
# by default, do not forward anything
proxy_redirect off;
}
location /longpolling {
proxy_pass http://127.0.0.1:8072;
#proxy_pass http://odoochat;
}
location ~* .(js|css|png|jpg|jpeg|gif|ico)$ {
expires 2d;
proxy_pass http://127.0.0.1:8069;
add_header Cache-Control "public, no-transform";
}
# cache some static data in memory for 60mins.
location ~ /[a-zA-Z0-9_-]*/static/ {
proxy_cache_valid 200 302 60m;
proxy_cache_valid 404 1m;
proxy_buffering on;
expires 864000;
proxy_pass http://127.0.0.1:8069;
}
}
}
And if I don't use my api too many times, things are normal.
If you want nginx to rewrite the url directly you can remove this line:
proxy_redirect off;
Please check documentation: https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect
Otherwise it's an issue with your browser not nginx.
I am quite new to NGINX configuation. I have looked through many posts looking for someone else who ran into this problem and found a working solution.
I have 2 node.js web-applications that will run on 2 separate ports (81, 82 for example).
My initial intention was to use a reverse proxy so that visitors could hit the same physical box, but be served different content based on the domain they used.
I was successful in having to separate sites render based on their domain. One of the applications has a subdomain tied to it (app.exampleb.com) and it seems that any time I try to access that subdomain, nginx serves me a page stating "that I have configured the server successfully"...... Thanks nginx.
I have placed the following in my nginx.conf file:
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;
# server_name_in_redirect off;
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";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
##
# nginx-naxsi config
##
# Uncomment it if you installed nginx-naxsi
##
#include /etc/nginx/naxsi_core.rules;
##
# nginx-passenger config
##
# Uncomment it if you installed nginx-passenger
##
#passenger_root /usr;
#passenger_ruby /usr/bin/ruby;
server {
listen 80;
server_name examplea.com;
location / {
proxy_pass http://localhost:81;
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;
}
}
server {
listen 80;
server_name app.examplea.com;
location / {
proxy_pass http://localhost:81;
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;
}
}
server {
listen 80;
server_name exampleb.com;
location / {
proxy_pass http://localhost:82;
}
}
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
The full config should be this:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://localhost:81;
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;
}
}
Place it inside the http {} directive in nginx.conf
Remember to restart nginx
I have node.js server running locally on windows 7 and would like to add Nginx server to serve static files.
After downloading and extracting Nginx, when I run nginx.exe, Nginx is accessible to node only inside the nginx folder.
How can I make it run automatically and accessible globally in node?
Going over various tutorials, they describe the configuration for linux but I assume there are differences in the .conf file? If so, what are they?
Edit
Using express.js
I keep getting 404 when trying to serve a css file...
Here is the request URL: 127.0.0.1:3000/public/css/style.css
And here is the config file:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 64;
sendfile on;
keepalive_timeout 65;
server {
listen 0.0.0.0:80;
server_name localhost;
access_log path_to_log;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
}
location ~* ^.+.(jpg|jpeg|gif|css|png|js|ico|xml)$ {
# access_log off;
root path_to_local_folder_with_static_files;
expires 15d;
}
}
I am having a weird issue with nginx throwing a 404 error on javascript files.
See it here:
http://ec2-54-85-163-197.compute-1.amazonaws.com/
Because the meteor application throws a 404, press escape and view the source of the page because meteor has javascript that redirects the entire page if javascript fails to load (A complete fail by meteor IMHO)
You will find that I am properly proxying from port 3000 running node to port 80 through the rendering of the main page.
If you go to http://ec2-54-85-163-197.compute-1.amazonaws.com:3000/, you will see the hello world render just fine.
My /etc/nginx/nginx.conf is the default conf file.
Here is the file /etc/nginx/sites-available/ec2-54-85-163-197.compute-1.amazonaws.com
server {
listen 80;
server_name ec2-54-85-163-197.compute-1.amazonaws.com;
root /home/meteor/can_i_help_you/can_i_help_you;
access_log "/home/ubuntu/logs/access.log";
error_log "/home/ubuntu/logs/error.log" error;
charset utf-8;
default_type application/octet-stream;
sendfile on;
# would be awesome if your mobile-app can utilize keep-alives!
keepalive_timeout 65;
# enable gzip
gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length 1000;
gzip_proxied any;
gzip_buffers 16 8k;
# we only gzip these mime-types (since there's no use to gzip jpegs)
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
# tell-client to cache all 'assets'
location ~* \.(?:jpg|jpeg|gif|png|ico|gz|svg|svgz|mp4|ogg|ogv|webm)$ {
expires 1M;
access_log off;
add_header Cache-Control "public";
}
# disable logging for some `common` files
# Disable logging for favicon
location = /favicon.ico {
log_not_found off;
access_log off;
}
# Disable logging for robots.txt
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
# Prevent clients from accessing hidden files (starting with a dot)
location ~* (^|/)\. {
return 403;
}
# Prevent clients from accessing to backup/config/source files
location ~* (\.(bak|config|sql|fla|psd|ini|log|sh|inc|swp|dist)|~)$ {
return 403;
}
# reverse-proxy here, if your have multiple machine/cores would be better to use UPSTREAM so nginx can load-balance requests
#try_files $uri $uri/ #silly;
location / {
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_redirect off;
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 $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Connection "";
proxy_pass http://ec2-54-85-163-197.compute-1.amazonaws.com/;
}
#location / {
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header Host $http_host;
# proxy_set_header X-NginX-Proxy true;
#
# proxy_pass http://ec2-54-85-163-197.compute-1.amazonaws.com:3000/;
# proxy_redirect off;
#}
}