nginx with nagvis configuration rewrite - configuration

I want to run nagvis on nginx.
My current configuration looks like this:
# Nagvis Configuration
location /nagvis {
alias /opt/nagvis/share;
}
location ~ ^(/nagvis/.*\.php)(.*)$ {
root /opt/nagvis/share/;
include fastcgi_params;
fastcgi_index index.php;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
fastcgi_pass unix:/var/run/fcgiwrap-php.sock;
}
The requested URL is: ..localhost/nagvis/index.php
Directed to the filesystem: /opt/nagvis/share
But then the index.php includes config.php which redirects to
..localhost/frontend/nagvis-js/index.php which results in an 404 file not found error. The logs tells me that it is looking for a total different place.
So why does it delete the "nagvis" url part, and how to get a correct rewrite for that?

Related

HTML files are downloaded instead of rendered in browser using NGINX

My contact.html file is being downloaded rather than rendered in the browser when running my site on NGINX.home.html is working properly. This is how my default (in folder sites-available) file looks like:
server {
listen 90;
listen [::]:90;
server_name example.com;
root /home/myname/www;
location / {
try_files $uri /home.html;
add_header Access-Control-Allow-Origin *;
}
location = /contact {
default_type text/html;
alias /home/myname/www/contact.html;
}
}
When I add /contact to my url on my browser, contact.html gets downloaded as unknown file format. After having done an extensive search, these are the things I've tried:
Clear the browser cache (it also happens in Edge, so clearly this isn't the issue)
In nginx.conf I commented out the default_type application/octet-stream and un-commented default_type text/html
I have checked the in mime.types file the type text/html exists.
using try_files $uri /contact.html
Any help will be appreciated!
The issue was that default_type text/html property in nginx.conf lives in http {...} block. Since my server is listening to port 90 this configuration does not apply. once I changed the port to 80 the issue was resolved.

NGINX can not find Phpmyadmin Error 404 with Code-server

Hello guys I have a VPS with Ubuntu 20.04.
I have code-server installed and it works properly.
I also have Phpmy admin installed and mariadb, but when i go to my.ip.adress/phpmyadmin i get an 404 error.
I think the problems is that code-server tries to resolve the path or something like this.
My configuration looks like this:
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/html;
index index.php index.html index.htm index.nginx-debian.html;
server_name 178.254.34.3;
location / {
try_files $uri $uri/ =404;
}
location /phpmyadmin {
root /usr/share/phpmyadmin;
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
index index.php index.html index.htm index.nginx-debian.html;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
Does someone can help me to solve this problem ?
I tried almost everything i could find on the web and it didnt solved the problem.
So I found the problem and it was very obvius at the end. When i first installed code-server i had to create a specifig configuration. so first of all my server was never using the default configuration i posted here. I changed a few things and now it works. At least I am able to see the login page and the info.php file i created. Thanks for your help.

Nginx: try_files outside location

I'm configuring a pretty standard webserver using nginx. The server is working as expected, however, there is a small configuration detail I would like to understand.
My current configuration is:
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?q=$uri;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
With this configuration, if I access to: http://myweb.com/wp-content/uploads/2012/10/cropped-bitmap11.png/lol.php I get a 404 as expected.
However, with this configuration:
try_files $uri =404;
location ~ \.php$ {
fastcgi_index index.php;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include /etc/nginx/fastcgi_params;
}
I get a blank page with "Access denied".
Why is the result different?
Thank you
You are probably under the impression that try_files on server level must work for every request. Not at all. Quite the contrary, it works only for requests that match no location blocks.
short anwser: as of php5.3.9, php-fpm does not allow extensions other than .php and .php5 because of the default value for security.limit_extensions and your request as been made to an existent .png file.
long answer: this is nothing to do with try_files being inside or outside of a location block. let's break and explain:
request is: http://myweb.com/wp-content/uploads/2012/10/cropped-bitmap11.png/lol.php
on your first configuration
it matches the location ~ .php$ { ... } block because the request ends with .php.
the try_files $uri =404; directive inside .php$ location causes nginx return 404 as there is no file named $uri (=/wp-content/uploads/2012/10/cropped-bitmap11.png/lol.php)
the location / { ... } block never matches. it is only matched if no other location blocks are matched. (see http://wiki.nginx.org/HttpCoreModule#location)
on your second configuration
again, as the request ends with .php, it matches the .php$ location block.
there is no check for a file existence inside the location block, the request is directly passed to the fastcgi process.
fastcgi process finds the /wp-content/uploads/2012/10/cropped-bitmap11.png (appearently, it exists) and denies to run the request, because of the .png extension. (see the short answer)
i don't know if it is a bug or a "by design" thing but, contrary to "root" directive, a try_files directive outside the location blocks does not inherited inside the location block. (somebody may correct this if it is wrong)

Deploying concrete5 on nginx

I have a concrete5 site that works 'out of the box' in apache server. However I am having a lot of trouble running it in nginx.
The following is the nginx configuration i am using:
server {
root /home/test/public;
index index.php;
access_log /home/test/logs/access.log;
error_log /home/test/logs/error.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# pass the PHP scripts to FastCGI server listening on unix socket
#
location ~ \.php($|/) {
fastcgi_pass unix:/tmp/phpfpm.sock;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
location ~ /\.ht {
deny all;
}
}
I am able to get the homepage but am having problem with the inner pages. The inner pages display an "Access denied". Possibly the rewrite is not working, in effect I think its querying and trying to execute php files directly instead of going through the concrete dispatcher.
I am totally lost here.
Thankyou for your help, in advance.
Changed the configurations to:
server {
root /home/test/public;
index index.php;
access_log /home/test/logs/access.log;
error_log /home/test/logs/error.log;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to index.html
try_files $uri $uri/ /index.php/$request_uri;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules
}
# pass the PHP scripts to FastCGI server listening on unix socket
#
location ~ \.php($|/) {
set $script $uri;
if ($uri ~ "^(.+\.php)(/.+)") {
set $script $1;
}
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$script;
fastcgi_intercept_errors on;
fastcgi_pass unix:/tmp/phpfpm.sock;
}
location ~ /\.ht {
deny all;
}
}
And it works thanks to hangover and the link he provided in serverfault.
I am still not clear what I did wrong, maybe an nginx expert can help me understand.

Can't get location blocks figured out in nginx

Having a hard time trying to figure out location blocks in the nginx config. This is what I have:
server {
listen 80;
server_name _;
access_log /var/log/nginx/example.com.access_log;
error_log /var/log/nginx/example.com.error_log warn;
root /var/www/root;
index index.php index.htm index.html;
fastcgi_index index.php;
location /wp/ {
root /var/www/wordpress;
index index.php index.htm index.html;
fastcgi_index index.php;
}
location ~* \.php$ {
try_files $uri =404;
keepalive_timeout 0;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
Browsing to / works as expected and shows the website in /var/www/root, but if locations work as I think they should browsing to /wp should take me to the wordpress installation in /var/www/wordpress. All I am getting is:
404 Not Found
nginx/0.7.67
If I relocate the /var/www/wordpress directory to /var/www/root/wordpress and browse to /wordpress all is perfect.
What am I doing wrong with the location block??
I've never configured nginx before and am a bit of a complete web newb anyway.
I want to be able to have more location blocks for other applications as well. This is really just a basic example for posting on here.
Updated nginx to version in Debian Squeeze backports. No improvement:
404 Not Found
nginx/1.1.19
The reason why it is not working is that ...
At the server level, you have "root /var/www/root". So basically, every location block will use this unless specifically overridden. This is good practice.
You have then overridden it in the "wp" location block to "/var/www/wordpress". However, the php location block is still using the default.
Now when you put in a request to "/wp/folder_a/file_a.php" which is physically located at "/var/www/wordpress/folder_a/file_a.php", the request hits the php location block and given the root folder active for that block goes to look for the file in "/var/www/root/folder_a/file_a.php". As a result, you get a "404 not found".
You can change the server level root directive to "/var/www/wordpress" and remove the override in the wp location. This will solve that problem but php scripts under "/var/www/root" will no longer work. Not sure if you have any.
If you need to run php under both "/var/www/root" and "/var/www/wordpress", you need to do this:
server {
...
root /var/www/root;
index index.php index.htm index.html;
# Keep fastcgi directives together under location
# so removed fastcgi_index
# Put keepalive_timeout under 'http' section if possible
location /wp/ {
root /var/www/wordpress;
# One appearance of 'index' under server block is sufficient
location ~* \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
location ~* \.php$ {
try_files $uri =404;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
}
That is, nest a duplicate php location block under the wp location block. It will inherit the root directive for wp.
To help keep things succint and ease edits etc, you can put the fastcgi directives into a separate file and include it in as needed.
So in /path/fastcgi.params, you have:
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
Your conf can then be:
server {
...
root /var/www/root;
...
location /wp/ {
root /var/www/wordpress;
location ~* \.php$ {
try_files $uri =404;
include /path/fastcgi.params;
}
}
location ~* \.php$ {
try_files $uri =404;
include /path/fastcgi.params;
}
}
In this way, if you need to edit any fastcgi param, you just edit it in the one place.
PS. Updating your nginx will not solve this as it is not a version issue .. but update anyway!