Can't access nginx server from outside [closed] - nginx

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.

Related

Nginx memory leak issue

We are facing issue with nginx memory leak it seems.
Setup:
Nginx running as deployment in GKE
Nginx version 1.20.2
Nginx is used to stream HLS. We write chunk file to a google filestore(NFS service). It is mounted on /var/www/html/.
Nginx never ever recovers memory it just grows on increasing. Nginx confiuration
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log notice;
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;
}
Default.conf
server {
listen 80;
server_name localhost;
proxy_buffering off;
proxy_buffer_size 4k;
proxy_buffers 64 4k;
proxy_busy_buffers_size 16k;
proxy_cache_valid 200 302 1m;
proxy_cache_valid 404 60m;
proxy_cache_use_stale error timeout invalid_header updating;
proxy_redirect off;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' '*';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Access-Control-Max-Age' 1728000;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location /stub_status {
stub_status on;
access_log on;
allow all;
}
}
}
Screenshot of our internal monitoring system
First of all, what does your chart show? Memory usage of nginx worker processes? Or memory utilization of whole system?
In case of nginx memory growth it may relate to known issue (basically with OpenSSL either), see https://trac.nginx.org/nginx/ticket/2316
So either try to apply patch suggested by Maxim in that issue, or try the workaround he suggested in the last comment or upgrade to newer version of OpenSSL (PKCS11 engine) or even nginx (especially if it is linked statically).
There are enough OpenSSL-related leak issues, see also for example https://github.com/kubernetes/ingress-nginx/issues/7647 or linked within. So to veryfy it is not affected by OpenSSL, try to test it without SSL/TLS/https and check whether you'd see growth of memory usage.
Although I don't see any memory leak trying vanilla nginx 1.20.2 (without any patch, built with OpenSSL 1.1.1k) testing similar configuration (I don't see proxy_pass directive in your config so I was simply proxying to http/https upstream too). No leak reproducible at all.
In case of high system memory usage, it may be common OS cache or even some buffering of NFS, see https://askubuntu.com/a/1393696/1384131 for similar question.

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!

Basic Configuration of Golang App in 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.

Serving PHP files as downloads, instead of executing them

I recently installed nginx and php 7.0.16 in my machine, but for some reason nginx downloads php files, rather than executing them. I've already spent couple of days and implemented all solutions available online, but all in vain.
My nginx.conf is:
worker_processes 4;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
# Load dynamic modules. See /usr/share/nginx/README.fedora.
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;
include /etc/nginx/sites-enabled/*;
}
There is no file in conf.d folder and sites-enabled has only default file that looks like below
server {
listen 80;
server_name infrastructure;
root /home/infra/index;
index index.php index.html index.htm;
#return 301 https://$server_name$request_uri;
location / {
try_files $uri $uri/ = 404;
}
# pass the PHP scripts to FastCGI server listening on the php-fpm socket
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
# With php5-cgi alone:
#fastcgi_pass 127.0.0.1:9000;
fastcgi_pass unix:/var/run/php-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
Can someone please advise, what could be the problem?
Found the solution. The problem was in nginx.conf file.
Replaced following line:
default_type application/octet-stream;
with:
default_type text/html;
Nginx is available as a package for Ubuntu 16.04 which we can install.
apt-get -y install nginx
Start nginx afterwards:
service nginx start
Then open localhost page and see what comes up.
Install PHP 7
We can make PHP work in nginx through PHP-FPM (PHP-FPM (FastCGI Process Manager) is an alternative PHP FastCGI implementation with some additional features useful for sites of any size, especially busier sites) which we install as follows:
apt-get -y install php7.0-fpm
PHP-FPM is a daemon process (with the init script php7.0-fpm) that runs a FastCGI server on the socket /run/php/php7.0-fpm.sock.
The nginx configuration is in /etc/nginx/nginx.conf which we open now:
nano /etc/nginx/nginx.conf
The configuration is easy to understand (you can learn more about it here: http://wiki.nginx.org/NginxFullExample and here: http://wiki.nginx.org/NginxFullExample2)
First (this is optional) adjust the keepalive_timeout to a reasonable value:
[...]
keepalive_timeout 2;
[...]
The virtual hosts are defined in server {} containers. The default vhost is defined in the file /etc/nginx/sites-available/default - let's modify it as follows:
nano /etc/nginx/sites-available/default
[...]
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
include snippets/fastcgi-php.conf;
# With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# With php7.0-fpm:
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
[...]
server_name _; makes this a default catchall vhost (of course, you can as well specify a hostname here like www.example.com).
root /var/www/html; means that the document root is the directory /var/www/html.
The important part for PHP is the location ~ .php$ {} stanza. Uncomment it to enable it.
Now save the file and reload nginx:
service nginx reload
Next open /etc/php/7.0/fpm/php.ini...
nano /etc/php/7.0/fpm/php.ini
... and set cgi.fix_pathinfo=0:
[...]
; cgi.fix_pathinfo provides *real* PATH_INFO/PATH_TRANSLATED support for CGI. PHP's
; previous behaviour was to set PATH_TRANSLATED to SCRIPT_FILENAME, and to not grok
; what PATH_INFO is. For more information on PATH_INFO, see the cgi specs. Setting
; this to 1 will cause PHP CGI to fix its paths to conform to the spec. A setting
; of zero causes PHP to behave as before. Default is 1. You should fix your scripts
; to use SCRIPT_FILENAME rather than PATH_TRANSLATED.
; http://php.net/cgi.fix-pathinfo
cgi.fix_pathinfo=0
[...]
Reload PHP-FPM:
service php7.0-fpm reload
Now create the following PHP file in the document root /var/www/html:
nano /var/www/html/info.php
<?php
phpinfo();
?>
Now we call that file in a browser (e.g. http://localhost/info.php):
When using php-fpm, i uncommented this bloc in /etc/nginx/sites-available/default
location ~ \.php$ {
include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# # With php-cgi (or other tcp sockets):
#fastcgi_pass 127.0.0.1:9000;
}
you'd need to set a location block for PHP as you did in the first
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
No need to remove php handlers,
Comment out or remove the line
#php_admin_value engine Off
it should work.

Getting error 502 instead of displaying errors on nginx + php-fpm

I've tried everything and i can't still get to display errors on the browser, I'm using nginx, php-fpm and centOS 6.4
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;
}
this is my nginx config inside conf.d
server {
listen 80;
server_name localhost;
root /server/public;
index run.php;
location / {
try_files $uri $uri/ /run.php;
}
location ~ \.php$ {
fastcgi_index run.php;
fastcgi_pass 127.0.0.1:9000;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
php-fpm config
catch_workers_output = yes
php_flag[display_errors] = on
php_admin_flag[log_errors] = on
php.ini
display_errors = on
log_errors = on
I'm running CentOS 6.4 on virtualbox with NAT enabled, if i curl localhost from inside the server i get the correct error but if i access the site from the browser outside of the server i get a 502 bad gateway error, if the page I'm loading has no errors everything works fine, am I missing anything?
If you're wondering why you were getting that error I could give you few hints.
error 502 means bad gateway, means that the php server that nginx was trying to proxy to wasn't responding, maybe because php5-fpm wasn't listening to port 9000 and it was using a sock file instead, or could be that php wasn't running at all(if all php files aren't working)
Why did you get different responses from curl and the browser?, well because your server is defined as localhost I'm assuming both methods were not being captured by different server blocks.
The correct method to have fixed this is to check the active virtual hosts, and the php5-fpm listen config,
I think it's just coincidence that install the new php fixed the issue probably because it overwritten the old config file to a new one that worked with the nginx config.
But anyway, I was just trying to give you some hints of what to check if you ever find a similar problem in the future.
Looks like it was my version of php (5.3) that was failing, after updating to php 5.5 it started working

Resources