Nginx rewrite localhost to /usr/share/nginx/static - nginx

I want every request for http://localhost/static/ to be directed to /usr/share/nginx/static.
For example, it the user requests http://localhost/static/style/style.css, I want the server to reply with the content of /usr/share/nginx/static/style/style.css.
This is my code (which does not work):
server {
server_name http://localhost/;
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
}
And this is the main config file:
worker_processes 1;
events {
worker_connections 200;
}
http {
# Basic Settings
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;
# Logging Settings
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Include Other Configs
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Update: I have a file named mysite.conf like this which does not rewrite the urls as i mentioned in the question.
location /static {
root /usr/share/nginx/static;
}

Replace
server_name http://localhost/;
by
server_name localhost;
Per https://nginx.org/en/docs/http/ngx_http_core_module.html#server_name after the server_name directive you put the server's name, not an URL.
Also rewrite is to do HTTP redirections, so instead of
rewrite ^/static/(.*) /usr/share/nginx/$1 permanent;
use:
location /static {
root /usr/share/nginx;
}
See this other answer for another example: Use nginx to serve static files from subdirectories of a given directory

Related

Redirect NiFi Azure AD OAuth Request usinig NGINX

I have been plugging away at this for a good number of days now and I have been unable to find a solution. We have deployed NiFi onto an HDInsight cluster Edge Node, which comes with NGINX (free) pre-installed. We have installed NiFi onto this node and had the basic, unsecured NiFi UI being served by NGINX. So far, so good.
The requirement is that NiFi be secured using Azure AD and it is this that is giving me issues. The problem is that no matter what I try, I am unable to configure NGINX such that it redirects the OAuth request from NiFi to https://login.microsoftonline.com. The closest I have got is to redirect to the required domain, and include the query string, but missing the location part of the URL (AD tenant ID), which means authentication fails.
So my question is how can I configure NGINX to correctly redirect the OAuth request? I am either able to get:
https://login.microsoftonline.com/?client_id=<ID>&response_type=code&scope=openid+email&state=4bibmlfesmgbmi9o1p8blibd4q&redirect_uri=https%3A%2F%2F<NODE HOSTNAME>%3A443%2Fnifi-api%2Faccess%2Foidc%2Fcallback
or
https://<NODE HOSTNAME>/<TENANT ID>/oauth2/v2.0/authorize?client_id=<ID>&response_type=code&scope=openid+email&state=4bibmlfesmgbmi9o1p8blibd4q&redirect_uri=https%3A%2F%2Fems-poc-tri-hdi-nfi.apps.azurehdinsight.net%3A443%2Fnifi-api%2Faccess%2Foidc%2Fcallback
I am unable to get:
https://login.microsoftonline.com/<TENANT ID>/oauth2/v2.0/authorize?client_id=<ID>&response_type=code&scope=openid+email&state=4bibmlfesmgbmi9o1p8blibd4q&redirect_uri=https%3A%2F%2Fems-poc-tri-hdi-nfi.apps.azurehdinsight.net%3A443%2Fnifi-api%2Faccess%2Foidc%2Fcallback
I have tried all sorts, but I have finally had to admit defeat. The current NGINX config is given below:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 1024;
include /etc/nginx/mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log notice;
rewrite_log on;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
#upstream nifi {
# server 10.1.0.10:9443;
# keepalive 512;
#}
server
{
listen 443 default_server;
#server_name localhost;
server_name ems-poc-tri-hdi-nfi.apps.azurehdinsight.net;
proxy_set_header X-ProxyHost ems-poc-tri-hdi-nfi.apps.azurehdinsight.net;
proxy_set_header X-ProxyScheme https;
proxy_set_header X-ProxyPort 443;
location = / {
return 301 https://10.1.0.10:9443/nifi;
}
location /nifi {
proxy_pass https://10.1.0.10:9443/nifi;
}
location /nifi-api {
proxy_pass https://10.1.0.10:9443/nifi-api;
}
location ~ "\/<AAD TENANT ID>" {
access_log /var/log/nginx/special.access.log;
proxy_set_header Host login.microsoftonline.com;
add_header X-uri "$uri";
add_header X-host "$host";
add_header X-request_uri "$request_uri";
add_header X-args "$args";
if ($request_uri ~ "/[^?]+\?[^?]+callback$") {
#return 307 https://login.microsoftonline.com$uri?$args&redirect=true;
#rewrite ^(?<location>/[^?]+) $location redirect;
#rewrite ^(?<location>/[^?]+) https://login.microsoftonline.com$location redirect;
rewrite ^ $scheme://login.microsoftonline.com$uri?$args&redirect=true? last;
}
if ($request_uri ~ "/[^?]+\?[^?]+true$") {
# proxy_pass https://login.microsoftonline.com;
rewrite ^ https://login.microsoftonline.com last;
#return 307 https://login.microsoftonline.com$uri?$args;
}
}
}
}

nginx server configuration : rewrite to local folder

I am curently configuring a nginx server with subdomains.
I am using noip.com for DNS service, which provide me a web adress similar to
mydomain.ddns.net
As I have subdomains, I want to access them by the adress http://mydomain.ddns.net/subdomain
In the server, the subdomain files are located here :
/var/www/mydomain.ddns.net/www/subdomain
My question is : what is the code to write in the mydomain.ddns.net nginx configuration file to redirect http://mydomain.ddns.net/subdomain to /var/www/mydomain.ddns.net/www/subdomain/welcome.php ?
Thank you in advance for your help
Quentin C
I'm not an expert of serving PHP with nginx, and I can't try this right now, but at least the configuration below should get you started.
Try to follow this guide to tweak it, while this should make searching the configuration docs a bit less painful.
user nginx nginx;
worker_processes 3;
worker_rlimit_nofile 2048;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
accept_mutex on;
use epoll; # for Linux
}
http {
server_tokens off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log combined;
sendfile on;
server {
listen 80;
server_name mydomain.ddns.net;
# To enable https
#
# listen 443 ssl;
# ssl_certificate /etc/nginx/sslfiles/certificate_chain.crt;
# ssl_certificate_key /etc/nginx/sslfiles/certificate_key_no_passphrase.key;
# ssl_session_cache shared:a_cache_name:1m;
# ssl_session_timeout 5m;
root /var/www/mydomain.ddns.net/www;
location / {
return 403; # forbidden
}
location #php_app {
fastcgi_split_path_info ^(.+?(\.php)?)(\/.*)?$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
}
location /subdomain_one/ {
try_files $uri/index.html #php_app;
}
}
}

NGINX Location Directives Not Responding on Service Restart

I have a medium to large size WordPress site running off a MediaTemple NGINX server, and CENTOS and I'm having trouble getting any location block properties applied. What the goal is, is to have a directory locked down so that only the server has access to it. From everything I've seen, the root I'm setting and the locations blocks are being called correctly, they just don't seem to be being noticed.
#user nginx;
worker_processes 24;
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
# rewrite_log on;
include mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
#tcp_nopush on;
keepalive_timeout 3;
#tcp_nodelay on;
#gzip on;
#gzip_disable "MSIE [1-6]\.(?!.*SV1)”;
index index.php index.html index.htm
server_tokens off;
proxy_buffer_size 128k;
proxy_buffers 4 256k;
proxy_busy_buffers_size 256k;
include /etc/nginx/conf.d/*.conf;
include fastcgi.conf;
server {
listen 80;
server_name domainname.com;
rewrite ^ $scheme://www.domainname.com$request_uri redirect;
}
server {
listen 80;
server_name www.domainname.com;
root /var/www/vhosts/domainname.com/httpdocs;
# Additional rules go here.
location ^~ /protected-folder/ {
allow 127.0.0.1;
deny all;
}
# include global/restrictions.conf;
location ~* \.php$ {
try_files $uri =404; # This is not needed if you have cgi.fix_pathinfo = 0 in php.ini (you should!)
fastcgi_pass 127.0.0.1:9000;
}
# Only include one of the files below.
include global/wordpress.conf;
# include global/wordpress-ms-subdir.conf;
# include global/wordpress-ms-subdomain.conf;
}
}
After every change I'm running:
sudo service nginx restart
Do I have to do a full server reboot?
Is there something wrong with the syntax above?
For any imports above, the content follows almost identical to the WordPress article on setting up NGINX for WordPress.
Any help on this would be appreciated.

Nginx configuration on Centos

I have purchased New VPS from weloveservers provider . My VPS operating system is Centos 7 . My allocated IP is 192.3.108.207 . I have installed Nginx using ssh . Now i need to show something on my IP address . How can i point my IP to specific folder .. My current configuration for nginx is as follows ,
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
worker_connections 1024;
}
http {
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;
}
Here is my virtual.conf
server {
listen 80;
server_name 192.3.108.207;
root /var/www;
#charset koi8-r;
#access_log /var/log/nginx/log/host.access.log main;
location / {
root /var/www;
index index.php index.html index.htm;
try_files $uri $uri/ /index.php?$args;
}
}
You can specify the particular folder in your virtual.conf.
As you have mentioned in your virtual.conf root /var/www;
Instead of /var/www; , you can give the folder name and restart your nginx service.

nginx location index directive not working

I'm new to nginx and I just can't determine why my nginx config doesn't work as expected. All I want to do is to make nginx prioritize index.html over index.php for every web root (/) request.
This is my nginx config:
user www-data;
worker_processes 4;
pid /var/run/nginx.pid;
events {
worker_connections 768;
multi_accept on;
}
http {
##
# Basic Settings
##
server {
location / {
index index.html index.php;
}
location ~ \.php$ {
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME
$document_root$fastcgi_script_name;
include fastcgi_params;
}
}
sendfile on;
tcp_nopush on;
tcp_nodelay off;
keepalive_timeout 15;
keepalive_requests 100000;
types_hash_max_size 2048;
client_body_in_file_only clean;
client_body_buffer_size 32K;
client_max_body_size 300M;
server_tokens off;
# server_names_hash_bucket_size 64;
# server_name_in_redirect off;
include /etc/nginx/mime.types;
default_type application/octet-stream;
----------------- cut ---------------
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
Where's my error? What's the correct way to write this nginx config?
If you explicitly request /index.html, is it served? If not, you might want to add an explicit root /path/to/root; to your server {} block. Also verify that index.html has the correct permissions.
This will help with troubleshooting: It will force a 404 if the root index.html is not found. If that happens, at least you can check the logs to see were it was looking:
location = / {
index index.html;
}
Also, be sure to do nginx -s reload when changing the config.
Convention:
You should keep location and server declarations in virtual host files (/etc/nginx/conf.d/*.conf; and /etc/nginx/sites-enabled/*;, as you can see from the nginx conf). Files in /etc/nginx/conf.d/*.conf; are typically symlinked to files in /etc/nginx/sites-enabled/*; in order to become "enabled"
Some things to try
See my blog post here which has a setup similar to yours.
Try moving your index index.html index.html index.php files directive outside of a location {} block

Resources