Restart nginx: unknown directive "listen" - amazon-web-services

I've seen that there are already posts on this subject but despite the attempts I still can't do it.
I want to launch nginx via an AWS ec2. When downloading nginx I didn't have the sites-enabled and sites-workable folders, so I followed the instructions here (nginx missing sites-available directory) by creating the files and adding the include line in the http of nginx.config. I also created the symbolique link.
But without success... I have the following error when i restart the server with sudo /etc/init.d/nginx restart
"Starting nginx: nginx: [emerg] unknown directive "listen:" in /etc/nginx/sites-enabled/react-flask-app.conf:2"
This is my /etc/nginx/sites-enabled/react-flask-app.con file:
server {
listen: 80;
root /home/ec2-user/cecs470-web-app-project-team2/test/testest/build;
index index.html;
location: / {
try_files $uri $uri/ =404;
}
}
And this is my /etc/nginx/nginx.conf 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/doc/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;
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/sites-enabled/*;
include /etc/nginx/conf.d/*.conf;
index index.html index.htm;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name localhost;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
}
# redirect server error pages to the static page /40x.html
#
error_page 404 /404.html;
location = /40x.html {
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.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;
#}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2 default_server;
# listen [::]:443 ssl http2 default_server;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# # 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_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;
#
# location / {
# }
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
Do you have any leads? Thank you

The listen directive from the following file
my /etc/nginx/sites-enabled/react-flask-app.con
Should be
listen 80;
Yours has :

Related

Setting Up SubDomain on nginx

i installed nginx on amazon EC2 and the nginx page is showing correctly after correct install, i want to install a subdomain on this web server only, the domain is elsewhere, what should i do. where to start ? i already on my domain's DNS put A record( the IP of this new server).
here is the config 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 /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/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;
types_hash_max_size 4096;
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;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}

How to solve (unix:/home/richard/www/firstsite.sock failed (13: Permission denied) while connecting to upstream)?

I am trying do do this tutorial in a CentOS 7:
https://www.digitalocean.com/community/tutorials/how-to-serve-django-applications-with-uwsgi-and-nginx-on-centos-7
I want to test a Django basic project with Nginx, uWsgi into a CentOS 7.
But in the browser results this error:
502 Bad Gateway
nginx/1.20.1
Error502
firstsite.ini
[uwsgi]
project = firstsite
username = richard
base = /home/%(username)
chdir = %(base)/%(project)/Documentos/desenvolvimento/nginxteste/%(project)
home = %(base)/Env/%(project)
module = %(project).wsgi:application
master = true
processes = 5
uid = %(username)
socket = /home/richard/www/%(project).sock
chown-socket = %(username):nginx
chmod-socket = 660
vacuum = true
uwsgi.service
[Unit]
Description=uWSGI Emperor service
[Service]
ExecStartPre=/usr/bin/bash -c 'mkdir -p /home/richard/www/; chown richard:nginx /home/richard/www/'
ExecStart=/usr/bin/uwsgi --emperor /etc/uwsgi/sites
Restart=always
KillSignal=SIGQUIT
Type=notify
NotifyAccess=all
[Install]
WantedBy=multi-user.target
nginx.conf
# 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 /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/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;
types_hash_max_size 4096;
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 firstsite.com www.firstsite.com;
location = favicon.ico { access_log off; log_not_found off; }
location /static/ {
root /home/richard/Documentos/desenvolvimento/nginxteste/firstsite;
}
location / {
include uwsgi_params;
uwsgi_pass unix:/home/richard/www/firstsite.sock;
}
}
# server {
# listen 80;
# server_name secondsite.com www.secondsite.com;
#
# location = favicon.ico { access_log off; log_not_found off; }
# location /static/ {
# root /home/richard/Documentos/desenvolvimento/nginxteste/secondsite;
# }
#
# location / {
# include uwsgi_params;
# uwsgi_pass unix:/run/uwsgi/secondsite.sock;
# }
#
# }
server {
listen 80;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
}
Error in /var/log/nginx:
2022/05/13 14:03:36 [crit] 53778#53778: *1 connect() to
unix:/home/richard/www/firstsite.sock failed (13: Permission denied)
while connecting to upstream, client: 172.17.2.139, server:
firstsite.com, request: "GET / HTTP/1.1", upstream:
"uwsgi://unix:/home/richard/www/firstsite.sock:", host: "172.17.2.139"
I think its some permission, but I tried everything the error persist.
Can anyone help me?

Nginx " Cannot GET /api " Reverse Proxy

My problem:
I need to do a reverse proxy with Nginx in AWS Linux 2 (Free), and the nginx doesn´t stop throwing me Cannot GET /api ...
Code:
server {
location /api {
proxy_pass http://localhost:3000;
}
}
And nothing the only way this works is with
server {
location /api {
proxy_pass http://127.0.0.1:3000/;
}
}
But don't works the /api/something
Here my Nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
# Load dynamic modules. See /usr/share/doc/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;
types_hash_max_size 4096;
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;
listen [::]:80;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
error_page 404 /404.html;
location = /404.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers PROFILE=SYSTEM;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }
some commands used are:
+sudo iptables -A INPUT -i eth0 -p tcp –-dport 80 -m state –state NEW,ESTABLISHED -j ACCEPT
+sudo amazon-linux-extras install nginx1 -y

nginx using OR set at regex for the map

I'm trying to write a map for my website and it's working great:
map $request_uri $redirect_uri {
/en/oldname /en/newname;
/de/oldname /de/newname;
/fr/oldname /fr/newname;
}
until I try to implement some regex, something like this:
map $request_uri $redirect_uri {
/(?<lang>(en|de|fr))/oldname /$lang/newname;
}
The map above is not working, and due to the lack of debugging knowledge - I'm unable to know why. Even the basic regex (without using named captures) is not working for me:
map $request_uri $redirect_uri {
/(en|de|fr)/oldname /en/newname;
}
nginx 1.10.3
Please help me to figure out what am I doing wrong?
Mike, you should use “~” symbol to indicate a regular expression.
Look at here Module ngx_http_map_module
A regular expression should either start from the “~” symbol for a case-sensitive matching, or from the “~*” symbols (1.0.4) for case-insensitive matching. A regular expression can contain named and positional captures that can later be used in other directives along with the resulting variable.
The right configuration should be:
map $request_uri $redirect_uri {
~/(?<lang>(en|de|fr))/oldname /$lang/newname;
}
Good luck!
2017.07.13 edited
Here is the full configuration based on the default configuration(echo directive is provided by nginx-echo-module)
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
map $request_uri $redirect_uri {
~/(?<lang>(en|de|fr))/oldname /$lang/newname;
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
echo $redirect_uri;
}
#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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
And this is my test case:
yxr nginx # curl localhost/en/oldname
/en/newname
yxr nginx # curl localhost/de/oldname
/de/newname
yxr nginx # curl localhost/fr/oldname
/fr/newname
yxr nginx # curl localhost/cn/oldname
yxr nginx #
2017.07.14 edited
As #Mike pointed out, this requires at least nginx/1.11.0.
In regards to mononoke's answer. You don't need the extra set of parentheses in the pattern matching, and that could give some unexpected results for certain pcre engines as you are technically adding another group within the lang group.
~/(?<lang>en|de|fr)/oldname /$lang/newname;
map $http_user_agent $loggable {
"~kube-probe" 0;
default 1;
}

django: nginx static content doesn't work

I'm using django with nginx and gunicorn. nginx is supposed to serve the static content, but css, images and js files are not loaded in the browser. Why is that?
I've substituted my Django project's name with domain.
/etc/nginx/sites-enabled/domain.tld
server {
listen 80;
server_name 127.0.0.1;
access_log /srv/domain/access.log;
error_log /srv/domain/error.log;
location /static {
alias /srv/domain/collected_static;
}
location / {
proxy_pass http://127.0.0.1:8888;
}
}
/etc/nginx/conf/nginx.conf
user http;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
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 localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root 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 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;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
include /etc/nginx/sites-enabled/*;
}
gunicorn.conf.py
bind = "127.0.0.1:8888"
logfile = "/srv/domain/gunicorn.log"
loglevel = "info"
workers = 3
Excerpt from Django settings
DEPLOY_PATH = os.path.dirname(os.path.realpath(__file__))
STATIC_ROOT = os.path.join(DEPLOY_PATH, 'collected_static')
STATIC_URL = '/static/'
EDIT:
Output from the machine (links to pastebin):
ps aux | grep nginx
ls -l *.log
Your configuration looks correct. As long as the files are really collected, the Django and Gunicorn configurations have nothing to do with the static files serving. The following possibilities come to my mind:
The files are not collected into your collected_static directory yet (./manage.py collectstatic)
Nginx has no read access to the files
You use an old nginx version that has problems with your current configuration. You should use a current 1.x version, if you're on Debian, use the Deb repository from nginx.org.
If permissions aren't the problem, check the nginx access file to see if the requests are really reaching Nginx. Then check the nginx error log to see if any errors were logged.
As a sidenote (but unrelated), I recommend putting some proxy headers in your / location configuration and moving the app server configuration into a separate section, e.g.:
upstream app_server {
server localhost:8888 fail_timeout=0;
}
server {
...
location / {
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;
break;
}
}