I am currently wanting to use NGINX in my Rails setup. I have placed the configuration files in the directory RAILS_ROOT/config/nginx. Here is my config-file placed named development.conf and the mime.types-file.
I am wanting to place my logs in the RAILS_ROOT/log-directory.
This is my development.conf:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# main access log
access_log log/nginx.access.log;
# main error log
error_log log/nginx.error.log debug;
sendfile on;
keepalive_timeout 65;
server {
listen 9001; #omg!
server_name local.woman.dk;
rewrite ^/(.*)/$ /$1 last;
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_redirect off;
proxy_max_temp_file_size 0;
location / {
ssi on;
proxy_pass http://127.0.0.1:3000;
}
}
}
I am starting NGINX from my RAILS_ROOT with this command:
nginx -p . -c config/nginx/development.conf
And I get the following error:
nginx: [alert] could not open error log file: open() "./logs/error.log" failed (2: No such file or directory)
My version is this:
[(master)]=> nginx -v
nginx version: nginx/1.2.4
Am I doing anything wrong?
http://nginx.org/en/docs/ngx_core_module.html#error_log indicates that:
the default value is error_log logs/error.log error;
that for debug logging to work, nginx needs to be built with --with-debug.`
what's happening is that you're falling through to the default value, I'm not spotting any syntax errors so my guess is that your nginx is not compiled with --with-debug.
you can check that with the nginx -V (note: that's capital V)
On MAC it was in
/usr/local/logs/error.log
i found this after running:
nginx -V
configure arguments: --prefix=/usr/local --with-cc-opt=-Wno-deprecated-declarations --with-http_ssl_module --add-module=../nginx-rtmp-module/
Related
I am trying to install geoip module for nginx though dockerfile by adding to my dockerfile the following:
RUN apk add --no-cache libmaxminddb nginx-mod-http-geoip
RUN cd /var/lib; \
mkdir -p nginx; \
wget -q -O- https://dl.miyuru.lk/geoip/maxmind/country/maxmind.dat.gz | gunzip -c > nginx/maxmind-country.dat; \
wget -q -O- https://dl.miyuru.lk/geoip/maxmind/city/maxmind.dat.gz | gunzip -c > nginx/maxmind-city.dat; \
chown -R nginx. nginx
COPY nginx.conf /etc/nginx/nginx.conf
The nginx.config is the following:
load_module "modules/ngx_http_geoip_module.so";
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events{worker_connections 1024;
}
# See blow link for Creating NGINX Plus and NGINX Configuration Files
# https://docs.nginx.com/nginx/admin-guide/basic-functionality/managing-configuration-files/
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format kv 'site="$server_name" server="$host" dest_port="$server_port" dest_ip="$server_addr" '
'src="$remote_addr" src_ip="$realip_remote_addr" user="$remote_user" '
'time_local="$time_local" protocol="$server_protocol" status="$status" '
'bytes_out="$bytes_sent" bytes_in="$upstream_bytes_received" '
'http_referer="$http_referer" http_user_agent="$http_user_agent" '
'nginx_version="$nginx_version" http_x_forwarded_for="$http_x_forwarded_for" '
'http_x_header="$http_x_header" uri_query="$query_string" uri_path="$uri" '
'http_method="$request_method" response_time="$upstream_response_time" '
'cookie="$http_cookie" request_time="$request_time" category="$sent_http_content_type" https="$https"'
'geoip_country_name="$geoip_country_name"';
access_log /var/log/nginx/access.log kv;
sendfile on;
keepalive_timeout 65;
geoip_country /var/lib/nginx/maxmind-country.dat;
geoip_city /var/lib/nginx/maxmind-city.dat;
include /etc/nginx/conf.d/*.conf;
# The identifier Backend is internal to nginx, and used to name this specific upstream
upstream backend {
# dashboard is the internal DNS name used by the backend Service inside Kubernetes
server localhost:5005;
}
server {
listen 80;
root /usr/share/nginx/html;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
location /api/ {
resolver 127.0.0.11; #nginx will not crash if host is not found
# The following statement will proxy traffic to the upstream
proxy_pass http://backend;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
}
However, when I am inspecting the logs I am getting
geoip_country_name = "-"
Any idea of what is going wrong here? Could it be that I am running this locally?
The "-" is what the logfile uses when the value is empty. GeoIP uses the $remote_addr to calculate the source of the request.
172.17.0.1 is not a public IP address, it is an internal address of one of your proxy servers. Check the $http_x_forwarded_for header value for the real remote address (assuming your reverse proxy servers are configured correctly.
The Geoip module provides the geoip_proxy directive to ignore $remote_addr and use $http_x_forwarded_for instead.
For example (added to your other geoip_ directives):
geoip_proxy 172.17.0.1;
We were experiencing a similar problem.
It essentially came back to the points made by #RichardSmith, however in our case the following configuration resolved the problem:
geoip_proxy 0.0.0.0/0;
I have the current jenkins configuration:
server {
listen 80;
listen [::]:80;
server_name server_name mysubdomain.maindomain.com;
# This is the jenkins web root directory (mentioned in the /etc/default/jenkins file)
root /var/run/jenkins/war/;
access_log /var/log/nginx/jenkins/access.log;
error_log /var/log/nginx/jenkins/error.log;
#pass through headers from Jenkins which are considered invalid by Nginx server.
ignore_invalid_headers off;
location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
# rewrite all static files into requests to the root
# e.g /static/12345678/css/something.css will become /css/something.css
rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
}
location /userContent {
#have nginx handle all the static requests to the userContent folder files
#note : This is the $JENKINS_HOME dir
root /var/lib/jenkins/;
if (!-f $request_filename){
#this file does not exist, might be a directory or a /**view** url
rewrite (.*) /$1 last;
break;
}
sendfile on;
}
location #jenkins {
sendfile off;
proxy_set_header Host $host;
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;
# Required for new HTTP-based CLI
proxy_http_version 1.1;
proxy_request_buffering off;
proxy_pass http://127.0.0.1:2021;
}
location / {
# try_files $uri $uri/ =404;
try_files $uri #jenkins;
}
}
which is essentially a copy of this jenkins configuration and my current /etc/default/jenkins file:
NAME=jenkins
# location of java
JAVA=/usr/bin/java
JAVA_ARGS="-Djava.awt.headless=true"
# make jenkins listen on IPv4 address
JAVA_ARGS="-Djava.net.preferIPv4Stack=true"
PIDFILE=/var/run/$NAME/$NAME.pid
JENKINS_USER=$NAME
JENKINS_GROUP=$NAME
JENKINS_WAR=/usr/share/$NAME/$NAME.war
JENKINS_HOME=/var/lib/$NAME
RUN_STANDALONE=true
JENKINS_LOG=/var/log/$NAME/$NAME.log
MAXOPENFILES=8192
HTTP_PORT=2021
HTTP_HOST=127.0.0.1
# servlet context, important if you want to use apache proxying
PREFIX=/$NAME
JENKINS_ARGS="--webroot=/var/cache/$NAME/war --prefix=$PREFIX --httpListenAddress=$HTTP_HOST --httpPort=$HTTP_PORT"
a simple curl requests shows a response of Jenkins running:
$ curl http://localhost:2021/jenkins/
<html><head><meta http-equiv='refresh' content='1;url=/jenkins/login?from=%2Fjenkins%2F'/><script>window.location.replace('/jenkins/login?from=%2Fjenkins%2F');</script></head><body style='background-color:white; color:white;'>
Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Administer
-->
</body></html>
However I am unable to acess the Web UI from the browser. whenever I try to I get a 404. The following are the reevant versions of installed 'wares:
Nginx - 1.13.6
Jenkins - 2.73.2 (using java -jar path-to-warfile --version)
OS - ubuntu 16.04
JDK - openjdk version "1.8.0_131"
An inspection of sudo nginx -T revealed that my site config wasn't being loaded. After correcting the error in my nginx.conf (spelling error in the include directive for the directory), this resolved the issue.
Thanks to SmokedCheese on IRC for his/her help with this issue.
I have been trying to set up a local docker container that would host NGINX server. To start with, here is my Dockerfile:
# Set nginx base image
FROM nginx
# File Author / Maintainer
MAINTAINER myuser "myemail#mydomain.com"
# Copy custom configuration file from the current directory
COPY nginx.conf /etc/nginx/nginx.conf
I did build this file using the docker build command and when I listed the images, I get to see this image in the list.
Now, I tried to run this newly created image which resulted in an error:
my-MacBook-Pro:nginx-docker me$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
myrepo nginx-latest 0d73419e8da9 12 minutes ago 182.8 MB
hello-world latest c54a2cc56cbb 13 days ago 1.848 kB
nginx latest 0d409d33b27e 6 weeks ago 182.8 MB
my-MacBook-Pro:nginx-docker me$ docker run -it myrepo:nginx-latest
2016/07/15 07:07:35 [emerg] 1#1: open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
The path to the log file is configured in my nginx.conf which is as below:
#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;
server {
listen 8080;
server_name localhost;
root /Users/me/Projects/Sandbox/my-app;
#charset koi8-r;
access_log logs/host.access.log main;
#
# Wide-open CORS config for nginx
#
location / {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
}
# /api will server your proxied API that is running on same machine different port
# or another machine. So you can protect your API endpoint not get hit by public directly
location /api {
proxy_pass http://localhost:9000;
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;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header Access-Control-Allow-Origin *;
proxy_set_header Access-Control-Allow-Origin $http_origin;
}
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;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
include servers/*;
}
When I now tried to run this NGINX image, I get the following error:
2016/07/15 07:07:35 [emerg] 1#1: open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
nginx: [emerg] open() "/etc/nginx/logs/access.log" failed (2: No such file or directory)
What should I do to fix this? Also what and where should that path be? I suppose it is on the underlying OS path that is exposed by Docker?
The base image in your Dockerfile is nginx (nginx:latest to be exact). It has a pre-configured nginx configuration that comes from Debian Nginx package. You may inspect the container yourself: docker run -it --rm nginx /bin/bash and look at the files and directories to learn few facts about it:
it provides nginx user
it provides /var/log/nginx directory, but root owns it
it provides access.log and error.log in that directory writable by anyone
(Dockerfile for the base Nginx image is here)
Your configuration:
runs Nginx as nginx (it's the default)
tries to write log files into /etc/nginx/logs
Apparently, this directory does not exist because no one has created it. If it'd existed it should be writable by nginx user.
I have the following configuration on a NGINX which is serving as a reverse proxy to my Docker machine located at: 192.168.99.100:3150.
Basically, I need to hit: http://localhost:8150 and the content displayed has to be the content from inside the Docker.
The configuration bellow is doing his job.
The point here is that when hitting the localhost:8150 I'm getting http status code 302, and I would like to get the http status code 200.
Does anyone know if it's possible to be done on Nginx or any other way to do that?
server {
listen 8150;
location / {
proxy_pass http://192.168.99.100:3150;
}
}
Response from a request to http://localhost:8150/products
HTTP Requests
-------------
GET /projects 302 Found
I have found the solution.
Looks that a simple proxy_pass doens't work quite fine with ngrok.
I'm using proxy_pass with upstream and it's working fine.
Bellow my configuration.
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
upstream rorweb {
server 192.168.99.100:3150 fail_timeout=0;
}
server {
listen 8150;
server_name git.example.com;
server_tokens off;
root /dev/null;
client_max_body_size 20m;
location / {
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Frame-Options SAMEORIGIN;
proxy_pass http://rorweb;
}
}
include servers/*;
}
My environment is like this:
Docker (running a rails project on port 3150)
Nginx (as a reverse proxy exposing the port 8150)
Ngrok (exporting my localhost/nginx)
I'm trying to direct all HTTP requests resembling /<uuid4> to a specific HTTP server running on the localhost. Below is the relevant location line in my nginx.conf:
# nginx.conf
upstream django {
server unix:///app/django.sock; # for a file socket
}
server {
access_log /var/log/access.log;
error_log /var/log/error.log;
listen 80;
server_name 127.0.0.1;
charset utf-8;
client_max_body_size 75M;
# Django media
location /media {
alias /app/media;
}
location /static {
alias /app/static;
}
location ~* "[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$" { # matches UUIDv4
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_pass localhost:8000;
}
# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /app/conf/uwsgi_params;
}
}
When starting nginx, I get the following error: nginx: [emerg] unknown directive "8}-([0-9a-f]" in /etc/nginx/sites-enabled/nginx.conf:30
What gives?
Actually you have another error. I've checked your server block and got following:
$ sudo nginx -t
nginx: [emerg] invalid URL prefix in /etc/nginx/sites-enabled/test:23
nginx: configuration file /etc/nginx/nginx.conf test failed
This is error about missing protocol in proxy_pass localhost:8000; line. After fixing it to proxy_pass http://localhost:8000; configs test passed.
Probably you're looking into old (or wrong) error log.