Basic Configuration of Golang App in Nginx - nginx

I have a CentOS 7 server where I have running some Golang apps. As you know, every app is running on his own port, lets say: 9000,9100,9200 and so on.
Now, I have installed Nginx to serve all the websites, I have a domain for every site and I want to receive all the petitions in the port 80 and then just based on the domain i have to redirect to the application that corresponds.
By now,am trying to make it with one of the site that is running in the port 9094, I have no experience with Nginx so I was just reading to know what to do,but it seems like it's not working. in the file nginx.conf I added these lines:
server {
listen 80;
server_name mydomain.com;
access_log logs/mydomain.log main;
location / {
proxy_pass http://127.0.0.1:9094;
}
}
I have to mention that I didn't delete these lines that comes for default in the file:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
Is the configuration ok? and will allow me to add more sites? Thank you
If I ping to the domain everything is ok, but if I open the domain in the browser then I get status code 502
EDIT:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
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;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80;
server_name mydomain.com;
access_log logs/mydomain.log main;
location / {
proxy_pass http://127.0.0.1:9094;
}
}
}

Your server configuration looks okay and the 502 Status Code means you didn't configure the Go servers correctly. Specifically, Nginx did exactly what you expected it to, proxied the request to and from your upstream, but received an invalid response from your Go server.

Related

How to setup nginx to create a sub-domain?

I have one domain myartistbook.in, which is working fine. I want to create a sub-domain for it named adminpanel.myartistbook.in. The steps I followed are:
Added sub-domain name to /etc/hosts
127.0.0.1 localhost
***.***.***.180 myartistbook
***.***.***.180 adminpanel.myartistbook
edited /etc/nginx/nginx.conf:
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
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;
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name myartistbook.in www.myartistbook.in;
root /root/krim.com;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
server {
listen 80;
root /root/sites/adminpanel.com;
server_name adminpanel.myartistbook.in www.adminpanel.myartistbook.in;
index index.html;
location / {
try_files $uri $uri/;
}
}
}
Added a file named adminpanel.com inside /etc/nginx/sites-available.
server {
listen 80;
server_name www.adminpanel.myartistbook.in;
server_name ***.***.***.180;
root /root/sites/adminpanel.com;
index index.html;
access_log /var/log/nginx/adminpanel.com.access.log;
error_log /var/log/nginx/adminpanel.com.error.log;
location / {
try_files $uri /index.html =404;
}
}
Linked the above server block into /etc/nginx/sites-enabled
sudo ln -s /etc/nginx/sites-available/adminpanel.com /etc/nginx/sites-enabled/adminpanel.com
Restarted nginx with
sudo service nginx restart
While accessing the site I'm getting error adminpanel.myartistbook.in’s server IP address could not be found.
Am I missing any step?
Together with server-side configurations involving domains and subdomains in server_name, you need to make sure that your DNS records for myartistbook.in are properly configured as well.
Have you added a DNS record to point adminpanel.myartistbook.in to your server? You can either add an A record or a CNAME record for this. Looking at a quick check using whatsmydns.net for adminpanel.myartistbook.in, it seems you missed this step. Please check the documentation of your domain registrar on how to do this.
After adding the records, do a quick check again using whatsmydns.net if your CNAME/A record has taken effect.
In your step 3, modify the server_name to adminpanel.myartistbook.in without the www, unless this is actually your intention to use the whole www.adminpanel.myartistbook.in, which I don't think is the case here because you attempted to access it without the www:
adminpanel.myartistbook.in’s server IP address could not be found.
Also, it's probably good to use a different server {} block when referring to IP addresses as server_name then redirect to the domain/subdomain. However, redirecting from IP Address going to your domain/subdomain may be not be the priority now. Suggest to remove that line in the meantime.
Then run a quick nginx -T to check for errors in your configuration. If the check is successful, reload your nginx web server and try accessing the subdomain again.
Hope that helps!

Nginx Redirect of HTTP to HTTPS partially working

My objective is to redirect my web site like this:
http://EXAMPLE.com/ to https://EXAMPLE.com/ (working)
http://www.EXAMPLE.com/ to https://EXAMPLE.com/ (not working). This one is currently redirecting to https://www.EXAMPLE.com/.
In addition, even though I didn't define api.EXAMPLE.com in the server section below, when I enter the URL as http://api.EXAMPLE.com/, it gets redirected to https://api.EXAMPLE.com/. I believe this has to do with the DNS record resolving to the same IP address. I would rather it show page not found instead of redirecting.
DNS Records:
Type Name Value TTL
A # 35.161.XX.XX 600 seconds
A api 35.161.XX.XX 1 Hour
A www 35.161.XX.XX 1 Hour
And the Nginx configuration file:
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#keepalive_timeout 65;
keepalive_timeout 15;
types_hash_max_size 2048;
# Enable HTTPS by default on all our websites
#add_header Strict-Transport-Security "max-age=63072000; includeSubDomains";
#php max upload limit cannot be larger than this
client_max_body_size 40m;
include /etc/nginx/mime.types;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
index index.php index.html index.htm;
# Upstream to abstract backend connection(s) for PHP.
upstream php {
#this should match value of "listen" directive in php-fpm pool
server unix:/tmp/php-fpm.sock;
#server 127.0.0.1:9000;
}
# Redirect unsecured port 80 traffic (http://) to port 443 (https://)
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name EXAMPLE.com www.EXAMPLE.com;
#return 301 $scheme://EXAMPLE.com$request_uri;
#return 301 https://$host$request_uri;
return 301 https://EXAMPLE.com$request_uri;
}
#server {
# listen 80 default_server;
# listen [::]:80 default_server;
#server_name localhost;
# server_name EXAMPLE.com;
#root /usr/share/nginx/html;
# root /var/www/nginx;
# Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
#}
# Settings for a TLS enabled server.
server {
listen 443 ssl http2 default_server;
listen [::]:443 ssl http2 default_server;
server_name EXAMPLE.com;
root /var/www/nginx;
#For Basic Auth
auth_basic "Restricted";
auth_basic_user_file /var/www/nginx/.htpasswd;
#ssl_certificate "/etc/pki/tls/certs/EXAMPLE.crt";
#ssl_certificate_key "/etc/pki/tls/private/EXAMPLE.key";
ssl_certificate "/etc/letsencrypt/live/EXAMPLE.com/cert.pem";
ssl_certificate_key "/etc/letsencrypt/live/EXAMPLE.com/privkey.pem";
# It is *strongly* recommended to generate unique DH parameters
# Generate them with: openssl dhparam -out /etc/pki/nginx/dhparams.pem 2048
#ssl_dhparam "/etc/pki/nginx/dhparams.pem";
ssl_dhparam "/etc/pki/tls/dhparams.pem";
ssl_session_cache shared:SSL:1m;
ssl_session_timeout 10m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP;
ssl_prefer_server_ciphers on;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
}
}
Based on the lead from Richard, here is how I erased the HTTP Strict Transport Security history from Chrome:
In the Query Domain section, I entered my domain and sub-domains: example.com, www.example.com, db.example.com, api.example.com to see if they were listed.
Then I entered those I found into the Delete Domain section and pressed Delete.
I entered http://www.example.com into the browswer's location field and pressed Enter. It redirected to https://example.com.
I also tried http://db.example.com and http://api.example.com and both were redirected to https://example.com. I'm curious why these got redirected since I didn't have a server section for them in nginx.conf. Is it because it resolved to the same IP?
Lastly, I tried http://what.example.com and got a "Can't reach this page" error; which is right.
chrome://net-internals/#hsts

NGINX - 403 forbidden - Cannot serve static file from directory as root user

Running Centos7. I'm having a problem serving a static html file from a subdirectory. I've set all permissions to root and have set the user to root in the nginx.conf file. I've included my the config file and listing of the permissions for the directory that I'm trying to serve the html file from. The path and file I'm trying to serve from is /home/colin/
NGINX.CONF
user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
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;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 ipv6only=on default_server;
server_name _;
root /home/colin;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location /cap {
autoindex on;
try_files test.html /test.html;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
PERMISSIONS
f: /home/colin/test.html
drwxrwxrwx root root /
drwxr-xr-x root root home
drwxr-xr-x root root colin
-rwxr-xr-x root root test.html
I've tried multiple configurations with different permission settings, using aliases and different subdirectories with same root permission - and the same problem occurs: 403 Forbidden. Am I missing something obvious here? Any help would be greatly appreciated

Can't access nginx server from outside [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I recently had to set up a nginx server on a centOS 7 server.
In order to run the dataiku software.
Every thing seems to run fine but once i try to access the pages i get absolutely nothing.
With elinks in local i manage to get the nginx default web page but not from my browser so i think it comes frommy nginx configuration.
here is my nginx.conf :
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
And here is the default.conf included file :
server {
listen 80 default;
server_name _;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
I really need this server running properly and being accessible do you have any idea ?
Thank you for reading.
you should add a new rule on public zone, because CentOS 7 has a firewalld.
Try:
firewall-cmd --zone=public --add-service=http
and go head!
Add the rule to the permanent set and reload FirewallD:
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --reload
That should work!
You missed proxy_pass configuration which is actually translate all requests from backend to the outside via HTTP port 80 in your case:
server {
# Host/port on which to expose Data Science Studio to users
listen 80;
server_name _;
location / {
# Base url of the Data Science Studio installation
proxy_pass http://DSS_HOST:DSS_PORT/;
proxy_redirect off;
# Allow long queries
proxy_read_timeout 3600;
proxy_send_timeout 600;
# Allow large uploads
client_max_body_size 0;
# Allow protocol upgrade to websocket
proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
Official documentation is pretty clear on that: http://doc.dataiku.com/dss/latest/installation/reverse_proxies.html
Make sure you have uptodated Nginx to be able to serve WebSocket requests.

First time running a ubuntu server is it possible I have nginx installed in two places?

I have two paths:
/etc/nginx/sites-enabled
/opt/nginx/conf/nginx.conf
Both contain an .conf file, is it possible I have nginx installed in two locations? When I edit the first /etc/ conf file, I can control what is served, it appears the /opt conf file is ignored. Why?
conf 1:
server {
listen 80;
server_name www.mydomain.com;
access_log /var/www/mydomain/logs/access.log;
root /var/www/mydomain/;
index me.html me.js;
}
~
conf 2 (ignored at /opt/nginx/conf/nginx.conf
events {
worker_connections 1024;
}
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.3-p0/gems/passenger-3.0.11;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p0/ruby;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.mydomain.com;
root /var/www/hello/releases/current/public/;
passenger_enabled on;
}
server {
listen 80;
server_name localhost;
No, you only have one installation.
/opt/local/nginx/conf is where the base configuration for the nginx server is.
/etc/nginx/sites-enabled is where you'd symlink site you want to be active (from /etc/nginx/sites-available.
/etc/nginx/sites-available should contain the individual conf files for virtual hosts.
There should be one file in the sites-available folder (if you haven't added any more). That will be the one being used. It may not be named ending .conf

Resources