Route a url to a directory with nginx - nginx

Let's say this is the ip of a server running nginx:
1.2.3.4
Let's also say I've purchased this url:
www.abcd.com
I've edited the DNS records for www.abcd.com like so:
(Using Godaddy as a registrar)
A (host)
Host | Points To | TTL
# | 1.2.3.4 | Live!
Entering 1.2.3.4 into my browser's url bar will take me to my server's nginx welcome page. Because I pointed my url at the same address www.abcd.com also takes me to the same page, though it resolves to 1.2.3.4.
Here is my nginx config file:
sudo nano /opt/nginx/conf/nginx.conf #=>
http {
include /usr/local/nginx/conf/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
tcp_nodelay on;
gzip on;
server_names_hash_bucket_size 128;
client_max_body_size 4M;
client_body_buffer_size 128k;
include /usr/local/nginx/conf/conf.d/*.conf;
include /usr/local/nginx/conf/sites-enabled/*;
upstream appname {
server unix:///data/apps/appname/shared/tmp/puma/appname-puma.sock;
}
server {
listen 80;
server_name www.abcd.com;
root /public/rails/test;
# keepalive_timeout 5;
}
}
On my server, I have the following structure:
~/public/rails/test/index.html
Here's what index.html contains:
sudo nano /opt/nginx/conf/nginx.conf #=>
<h1> It works! Routing from <i>www.abcd.com</i> has been successful! </h1>
What I expect to happen:
Visiting www.abcd.com would be forwarded to 1.2.3.4 by the nameserver. When the request arrives at 1.2.3.4, nginx would see that it's come from www.abcd and serve this file to the user:
~/public/rails/test/index.html
In short:
www.abcd.com => It works! Routing from www.abcd.com has been successful!
What actually happens:
Visiting www.abcd.com is forwarded to 1.2.3.4 by the nameserver. However, it's although my config file isn't working. It just displays nginx' "welcome" message to the user, as if I haven't touched my config file.
I've tried
sudo service nginx stop
sudo service nginx start
and
sudo service nginx restart
and after editing the config, but different nothing happens.
What am I doing wrong? Where can I find logs to see exactly what's happening? What does nginx' welcome screen signify? That my paths are wrong? What? Flying blind here.

Your server blocks root directive is set to /public/rails/test (absolute path), while you say you expect it to serve ~/public/rails/test/index.html to the user. The character ~ indicates the current users home directory, and is a relative path.
Therefore: Try setting the absolute path (for example /home/myuser/public/rails/test) in the server block.

Add server_name with and without www
Add this line index index.html;
add ~ sign before /public/rails/test. Like: root ~/public/rails/test;
Try with this:
server {
listen 80;
server_name abcd.com www.abcd.com;
index index.html; # add this line
root ~/public/rails/test; # add "~" sign '/public/rails/test'
server_name www.abcd.com;
}

Related

nginx webdav could not open collection

I have built nginx on a freebsd system with the following configuration parameters:
./configure ... –with-http_dav_module
Now this is my configuration file:
user www www;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
# reserve 1MB under the name 'proxied' to track uploads
upload_progress proxied 1m;
sendfile on;
#tcp_nopush on;
client_max_body_size 500m;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#upload_store /var/tmp/firmware;
client_body_temp_path /var/tmp/firmware;
server {
server_name localhost;
listen 8080;
auth_basic "Restricted";
auth_basic_user_file /root/.htpasswdfile;
create_full_put_path on;
client_max_body_size 50m;
dav_access user:rw group:r all:r;
dav_methods PUT DELETE MKCOL COPY MOVE;
autoindex on;
root /root;
location / {
}
}
}
Now, the next things I do are check the syntax of the confiuration file by issuing a nginx -t and then do a graceful reload as follows: nginx -s reload.
Now, when I point my web-browser to the nginx-ip-address:8080 i get the list of my files and folders and so on and so forth (I think that is due to the autoindex on feature).
But the problem is that when I try to test the webdav using cadaver as follows:
cadaver http://nginx-ip-address:8080/
It asks me to enter authorization credentials and then after I enter that it gives me the following error:
Could not open Collection: 405 Not Allowed
And the following is the nginx-error-log line which occurs at the same time:
*125 no user/password was provided for basic authentication, client: 172.16.255.1, server: localhost, request: "OPTIONS / HTTP/1.1", host: "172.16.255.129:8080"
The username and pass work just fine wheni try to access it from the web-browser, then what is happening here?
It turns out that the webdav module in-built in nginx is broken and to enable full webdav, we need to add the following external 3rd party module: nginx-dav-ext-module.
Link to its github: https://github.com/arut/nginx-dav-ext-module.git
The configure parameter would now be:
./configure --with-http_dav_module --add-module=/path/to/the/above/module
The built in one just provides the PUT DELETE MKCOL COPY MOVE dav methods.
The nginx-dav-ext-module adds the following additional dav methods: PROPFIND OPTIONS
You will also need to edit the configuration file to add the following line:
dav_ext_methods PROPFIND OPTIONS;
After doing so check if the syntax of the conf file is intact by issuing: nginx -t
and then soft reload (gracefully) nginx: nginx -s reload
And Voila! you should now be able to use cadaver or any other dav client program to get into the directories.
I cannot believe that I solved this, it drove me nuts for a while!

Using gitlab's nginx to serve another app

Hello I have installed Gitlab using this
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md#installation
Now I want to use nginx to serve another content other than gitlab application
how can I do this
Where are the config files that I need to modify
How can I point a directory like /var/www so that nginx knows that is the root for another app.
Update(forgot to mention I'm running this under Red Hat 6.5, Debian/Ubuntu solution welcome)
Here I am using
- gitlab.example.com to serve gitlab.example.com over https.
- example.com over http to serve another content other than gitlab application.
Gitlab installed from deb package is using chef to provision ngnix, so you have to modify chef recipies and add new vhost template into chef cookbooks directory
You can find all chef cookbooks here:
/opt/gitlab/embedded/cookbooks/gitlab/
open
/opt/gitlab/embedded/cookbooks/gitlab/recipes/nginx.rb
change:
nginx_vars = node['gitlab']['nginx'].to_hash.merge({
:gitlab_http_config => File.join(nginx_etc_dir, "gitlab-http.conf"),
})
to:
nginx_vars = node['gitlab']['nginx'].to_hash.merge({
:gitlab_http_config => File.join(nginx_etc_dir, "gitlab-http.conf"),
:examplecom_http_config => File.join(nginx_etc_dir, "examplecom-http.conf"),
})
add this to the same file:
template nginx_vars[:examplecom_http_config] do
source "nginx-examplecom-http.conf.erb"
owner "root"
group "root"
mode "0644"
variables(nginx_vars.merge(
{
:fqdn => "example.com",
:port => 80,
}
))
notifies :restart, 'service[nginx]' if OmnibusHelper.should_notify?("nginx")
end
then in template directory(/opt/gitlab/embedded/cookbooks/gitlab/templates/default), create nginx vhost template file( nginx-examplecom-http.conf.erb) and add this there:
server {
listen <%= #listen_address %>:<%= #port %>;
server_name <%= #fqdn %>;
root /var/www/example.com;
access_log <%= #log_directory %>/examplecom_access.log;
error_log <%= #log_directory %>/examplecom_error.log;
location /var/www/example.com {
# serve static files from defined root folder;.
# #gitlab is a named location for the upstream fallback, see below
try_files $uri $uri/index.html $uri.html;
}
error_page 502 /502.html;
}
you have to set nginx['redirect_http_to_https'] = false in(/etc/gitlab/gitlab.rb):
external_url "https://gitlab.example.com"
gitlab_rails['gitlab_email_from'] = "info#example.com"
gitlab_rails['gitlab_support_email'] = "support#example.com"
nginx['redirect_http_to_https'] = false
nginx['ssl_certificate'] = "/etc/gitlab/ssl/ssl-unified.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/ssl.key"
gitlab_rails['gitlab_default_projects_limit'] = 10
add include <%= #examplecom_http_config %>; into /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx.conf.erb :
http {
sendfile <%= #sendfile %>;
tcp_nopush <%= #tcp_nopush %>;
tcp_nodelay <%= #tcp_nodelay %>;
keepalive_timeout <%= #keepalive_timeout %>;
gzip <%= #gzip %>;
gzip_http_version <%= #gzip_http_version %>;
gzip_comp_level <%= #gzip_comp_level %>;
gzip_proxied <%= #gzip_proxied %>;
gzip_types <%= #gzip_types.join(' ') %>;
include /opt/gitlab/embedded/conf/mime.types;
include <%= #gitlab_http_config %>;
include <%= #examplecom_http_config %>;
}
after all those changes run:
gitlab-ctl reconfigure
gitlab-ctl restart
vndr's above solution would work but on the https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/doc/settings/nginx.md, it said:
Inserting custom settings into the NGINX config
If you need to add custom settings into the NGINX config, for example
to include existing server blocks, you can use the following setting.
Example: include a directory to scan for additional config files nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
So let's check your /opt/gitlab/embedded/cookbooks/gitlab/templates/default/nginx.conf.erb to see if it contains: <%= #custom_nginx_config %>
(it looks like the current gitlab-7.5.3_omnibus.5.2.1.ci-1.el6.x86_64.rpm doesn't include it)
If not, then just add it above the line include <%= #gitlab_http_config %>; like:
<%= #custom_nginx_config %>
include <%= #gitlab_http_config %>;
Then open the /etc/gitlab/gitlab.rb to add:
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
We can make it simply by just add: include /etc/nginx/conf.d/*.conf; instead <%= #custom_nginx_config %>
Then create normal nginx .conf files in /etc/nginx/conf.d/ and gitlab-ctl reconfigure
As I did not wanted to change the config for gitlab Nginx server nor installing/configuring another Nginx and to make sure gitlab would survive an major update, I came to below solution for the Gitlab Omnibus package.
also as per
Gitlab:Ningx =>Inserting custom settings into the NGINX config
edit the /etc/gitlab/gitlab.rb of your gitlab:
nano /etc/gitlab/gitlab.rb
and sroll to nginx['custom_nginx_config'] and modify as below make sure to uncomment
# Example: include a directory to scan for additional config files
nginx['custom_nginx_config'] = "include /etc/nginx/conf.d/*.conf;"
create the new config dir:
mkdir -p /etc/nginx/conf.d/
nano /etc/nginx/conf.d/new_app.conf
and add content to your new config: /etc/nginx/conf.d/new_app.conf
server {
listen *:80;
server_name new_app.mycompany.com;
server_tokens off;
access_log /var/log/new_app_access.log;
error_log /var/log/new_app_error.log;
root /var/www/html/new_app/;
index index.html index.htm;
}
and reconfigure gitlab to get the new settings inserted
gitlab-ctl reconfigure
to restart nginx after changing your config's or adding more config's in /etc/nginx/conf.d:
gitlab-ctl restart nginx
to check nginx error log:
tail -f /var/log/gitlab/nginx/error.log
and see https://stackoverflow.com/a/39695791/6821811 for redirecting to another application server.
Even through you really can do it, a better practise would be to use upper level separate nginx server to serve both gitlab's nginx and your other custom content. Gitlab's nginx may change it's configuration at any time and it can break your custom content. Also, separate nginx is completely yours for configuring.
Just install those two instances to different ports and proxy gitlab's nginx with upper one. Of cause, it will be an overhead, but completely insignificant one.
I have tried both approaches, and the one that worked for me was to put a clean NGINX on top of gitlab's built in one. its more easy/convenient in the long run.
Depending on your needs here are some crucial things that have to be in place first:
DNS settings of your network/router/etc. (else this will not work, since the configurations here are based on server names,)
My setup is trivial one server, multiple sites hosted in the same server IP, and I filter by naming the apps thru NGINX name filter.
Here are the main steps to follow, keep in mind that depending on your needs this could imply more tweaking around, also this is a Ubuntu Server 14.04 .
First deactivate the main Nginx (the one bundled with omnibus) edit /etc/gitlab/gitlab.rb
nginx['enable'] = false
ci_nginx['enable'] = false
Now your free to install a clean instance of NGINX.
Regarding the previous step: sometimes the installer doesn't create the sites-enabled/ and sites-available/ folders, create them, and make sure to include them in the /etc/nginx/nginx.conf file
include /etc/nginx/sites-enabled/*.conf;
In a general nginx workflow you include your site configurations under sites-available/ and then when your ready/happy you make a link to sites-enabled/ folder and restart nginx so the changes are effective
Add your Gitlab configuration to Nginx site-available/ folder
here is my conf:
`upstream gitlab-workhorse {
server unix:/var/opt/gitlab/gitlab-workhorse/socket;
}
## Normal HTTP host
server {
## Either remove "default_server" from the listen line below,
## or delete the /etc/nginx/sites-enabled/default file. This will cause gitlab
## to be served if you visit any address that your server responds to, eg.
## the ip address of the server (http://x.x.x.x/)n 0.0.0.0:80 default_server;
#listen 0.0.0.0:80 default_server;
listen 0.0.0.0:80 ;
# listen [::]:80 default_server;
server_name gitlab.mycompany.com; ## Replace this with something like gitlab.example.com
server_tokens off; ## Don't show the nginx version number, a security best practice
root /opt/gitlab/embedded/service/gitlab-rails/public;
## See app/controllers/application_controller.rb for headers set
## Individual nginx logs for this GitLab vhost
access_log /var/log/nginx/gitlab.access.log;
error_log /var/log/nginx/gitlab.error.log;
location / {
client_max_body_size 0;
gzip off;
## https://github.com/gitlabhq/gitlabhq/issues/694
## Some requests take more than 30 seconds.
proxy_read_timeout 300;
proxy_connect_timeout 300;
proxy_redirect off;
proxy_http_version 1.1;
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-Forwarded-Proto $scheme;
proxy_pass http://gitlab-workhorse;
}
}
you can find more detail of the configuration in here more options
restart/reload nginx
sudo service nginx restart
restart gitlab omnibus and check your Gitlab configuration
sudo gitlab-ctl reconfigure
sudo gitlab-ctl tail
(just to check if something is wrong in your gitlab configuration)
Add extra (as many as you like) server configurations that you need in /etc/nginx/sites-available/ and eventually when happy/ready add the link to /etc/nginx/sites-enabled/
here is another example of another app
upstream app_server {
server 127.0.0.1:9080 fail_timeout=0;
}
server {
listen 80;
server_name jenkins.mycompany.com;
access_log /var/log/nginx/jenkins.access.log;
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://app_server;
break;
}
}
}
remember to always restart/reload nginx so that you see your changes.
sudo service nginx restart
check the logs in case something is wrong /var/log/nginx/anysites*.log
Please note that here we are using upstream with different ports and the names(They exist/are real/are registered in the domain of your company) are all pointing to the same IP address, meaning NIGNX will come and find the same IP address but it will not break because of the different ports in the upstreams this is really important
That's how my configuration is working right now I have not had any issues with Gitlab or any other apps.
So hopefully this will help anyone out there.
Those "other content" are declared in NGiNX with "Server Blocks".
The GitLab one is in /etc/nginx/sites-available/gitlab (according to the documentation, and symlined in [/etc/nginx/sites-enabled][3]).
You can add other server blocks in it, similar to this one (you may have to choose a different port number), as illustrated in this process (updated here for Ubuntu 14.04)
server {
listen 80; ## listen for ipv4; this line is default and implied
#listen [::]:80 default ipv6only=on; ## listen for ipv6
root /var/www/example.com/public_html;
index index.html index.htm;
# Make site accessible from http://localhost/
server_name example.com;
}
The root directive should reference the root folder of your webapp (/var/www or more likely a subfolder of /var/www).
That server block is quite separate from any GitLab config.

Is it possible to change server_tokens directive not in nginx.conf?

I need to uncomment the following line in nginx.conf
#server_tokens off;
But I don't want do it in nginx.conf directly so is it possible to make this change in another place like in some config files placed at /etc/nginx/conf.d/ directory?
This can be changed in either http, server or location block.
syntax: server_tokens on | off;
default: server_tokens on;
context: http, server, location
Enables or disables emitting nginx version in error messages and in the “Server” response header field.
Source: http://nginx.org/en/docs/http/ngx_http_core_module.html#server_tokens

ember.js application does not update hashtag part of URI with NGINX server

I have an ember.js application I developped on my local machine. I use a restify/node.js server to make it available locally.
When I navigate in my application, the address bar changes like this:
Example 1
1. http://dev.server:3000/application/index.html#about
2. http://dev.server:3000/application/index.html#/items
3. http://dev.server:3000/application/index.html#/items/1
4. http://dev.server:3000/application/index.html#/items/2
I try now to deploy it on a remote test server which runs nginx.
Although everything works well locally, I can navigate into my web application but the part of the URI that is after the hashtag is not updated.
In any browser: http://test.server/application/index.html is always displayed in my address bar. For the same sequence of clicks as in Exemple 1, I always have:
1. http://web.redirection/application/index.html
2. http://web.redirection/application/index.html
3. http://web.redirection/application/index.html
4. http://web.redirection/application/index.html
Moreover, if I directly enter a complete URI http://web.redirection/application/index.html#/items/1 the browser will only display the content that is at http://test.server/application/index.html (which is definitely not the expected behaviour).
I suppose this come from my NGINX configuration since the application works perfectly on a local restify server.
NGINX configuration for this server is:
test.server.conf (which is symlinked into /etc/nginx/sites-enabled/test.server.conf)
server {
server_name test.server web.redirection;
root /usr/share/nginx/test;
index index.html index.htm;
location / {
try_files $uri $uri/ /index.html;
}
location ~ \.csv$ {
alias /usr/share/nginx/test/$uri;
}
}
nginx.conf
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
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 debug;
gzip on;
gzip_disable "msie6";
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
EDIT:
Just to be sure that there were no missing files on my test server: I ran a restify/node server (like on my dev machine) and everything works fine when I connect to this server (!). Both nginx and restify servers points to the same files.
EDIT 2
I discovered that my problem happens when I use a web redirection.
If I use an address like http://test.server/application/index.html everything works fine
If I use http://web.redirection/application/index.html it does not work.
So this is my nginx conf that is not correctly redirecting web.redirection URI to test.server or something like that.
Does someone has an idea ? What do I miss ? What should I change to make this work ?
EDIT 3 and solution
The web redirection I used was an A type DNS record. This does not work. Using a CNAME type DNS record solves the issue.
No, this has nothing to do with nginx, any thing past the # is never sent to the server, a javascript code should handle this, I would suggest to use firebug or any inspector to make sure that all your js files are being loaded, and nothing fails with a 404 error, also check for console errors on the inspector console.
The problem came from the DNS redirection from web.redirection to test.server.
It was an A-type record: this does not work.
Using a CNAME-type record that points directly to test.server works.

NGINX [emerg] unknown directive "upload_pass" error in a config file

I have installed Nginx 1.2.0 with Passenger on my Mac Mini running Lion Server. I used the instructions from the link below.
https://github.com/coverall/nginx
I will state upfront that I am new to Nginx & Passenger. I am working on a Ruby on Rails project that I would like to host on the server. When I try to start Nginx I get the following error:
[emerg] unknown directive "upload_pass" in /usr/local/etc/nginx/virtualhosts/adam.localhost.coverallcrew.com.conf:20
Here are lines 19 & 20 from the file in question. This is a file that I assume was included in the Nginx installation. The only config file I have done anything with is nginx.conf where I added the lines to hopefully host my Rails application.
# pass request body to here
upload_pass #fast_upload_endpoint;
This is my second attempt at doing extensive web searches on how to correct this error. I had hoped to find if I needed to add something to nginx.conf or something to get upload_pass defined somewhere but only found solutions where the directive was indeed missing.
I took a look at nginx.conf. There are a lot of statements commented out. Here are the ones that are not:
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
gzip on;
server_name_in_redirect off;
port_in_redirect off;
client_max_body_size 8m;
client_body_buffer_size 128k;
include upstreams/*.conf;
include virtualhosts/*.conf;
include third-party/*.conf;
server {
listen 8080;
server_name www.lightbesandbox2.com;
root /Sites/iktusnetlive_ror/public;
passenger_enabled on;
}
}
Another question: Do I need these virtual hosts that were including in the Nginx install?
Any help would be appreciated.
It appears your Nginx is not compiled with the upload_pass module so it does not understand that directive. I am not certain how to do this with homebrew, but you can compile it in:
./configure --add-module=/path/to/upload_pass/source

Resources