FastCGI and Nginx wrong index files priority - mono

I want to set up a Mono ASP.NET on Nginx, but it seems the index priority does not work.
If I use the example from http://www.mono-project.com/FastCGI_Nginx as following:
location / {
root /srv/www/htdocs/;
index index.htm index.html default.aspx Default.aspx;
fastcgi_index Default.aspx;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
Then when I visit http://localhost/, it will always request Default.aspx, and if there is no such file, the web server will show a 404 error.
I tried to find answer by search the question, and I found this one:
asp mvc home root not working with mono, fastcgi and nginx
So I guess I meet the same issue, then I modified the config file as following:
location / {
root /srv/www/htdocs/;
index index.htm index.html default.aspx Default.aspx;
fastcgi_index /;
fastcgi_pass 127.0.0.1:9000;
include /etc/nginx/fastcgi_params;
}
The web server still requests .aspx-files as priority, even if I have only one .aspx file such as random.aspx in the htdocs folder, it will still show it before index.htm.
And then when I removed all FastCGI related config as following:
location / {
root /srv/www/htdocs/;
index index.htm index.html default.aspx Default.aspx;
}
Only in this way the index priority works correctly.
So who can tell me, why does this happen? Do I have to modify any config file that is related to FastCGI?

Make sure when your fastcgi settings in monoserve are pointing to a valid path.
fastcgi-mono-server2 /applications=www.domain1.xyz:/**:/var/www/www.domain1.xyz**/ /socket=tcp:127.0.0.1:9000
/var/www/www.domain1.xyz/ must point to the root directory of your web app.

Related

Nuxt.js app behind nginx reverse-proxy loading multiple pages at once

I've a nuxt.js app behind nginx reverse proxy. The nginx conf file looks like this:
server {
listen 80;
# Match *.lvh.me
server_name ~^(?<user>.+)\.lvh\.me$;
location / {
proxy_pass http://localhost:8080/sites/$user$uri$is_args$args;
}
location ~* \.(?:js|css|jpg|jpeg|gif|png|ico|cur|svg)$ {
proxy_pass http://localhost:8080;
}
}
As you can see I'm mapping all my site subdomains to a specific path on my site and it is working fine. I'm also mapping all assets to be loaded from the root (because otherwise it throws a 404 error).
The only issue I'm facing is the whenever I visit a subdomain e.g subdomain.lvh.me it loads two pages on top of each other, the original page from subdomain root (which is expected) but also the page from the main domain root i.e. lvh.me (which is not expected).
Can you please checkout my conf file to see if I'm doing anything wrong here?
So I've encountered this issue and what I did to fix it was to not rely on Nginx's root nor proxy_pass. Instead, I used a location block with an alias and a try_files inside like so:
location ^~ / {
alias /path/to/dist;
try_files $uri $uri/ /index.html = 404;
}

nginx different authentication for different directories

finding alternative of htaccess in apache for nginx, want to add different authentication rules for different directories. Different .htpasswd files for different directories. how to do it? Just like bitbucket does, it runs on nginx too.
I'm pretty sure that the authentication within BitBucket is handled by the application, there's no .htpasswd files anywhere to be found, authentication isn't handled by the web server.
However you can configure nginx to use a different .htpasswd for different paths using different location blocks for each path
location / {
root /var/www/app/;
index index.html index.php;
auth_basic_user_file /var/www/app/.htpasswd;
}
location /pathA {
root /var/www/app/pathA;
index index.html index.php;
auth_basic_user_file /var/www/app/pathA/.htpasswd;
}
or something to that effect

How do I interpret URLs without extension as files rather than missing directories in nginx?

So I'm trying to install ZoneMinder and have it run under Nginx, but it has a few compiled files that need to be run using Fast-CGI. These files lack an extension.
If the file has an extension, then there is no issue and Nginx interprets it as a file and will just return a 404 if it can't find it. If it has no extension, it will assume it's a directory and then eventually return a 404 when it can't find any sort of index page.
Here is what I have now:
# Security camera domain.
server {
listen 888;
server_name mydomain.com;
root /srv/http/zm;
# Enable PHP support.
include php.conf;
location / {
index index.html index.htm index.php;
}
# Enable CGI support.
location ~ ^/cgi-bin/(.+)$ {
alias /srv/cgi-bin/zm/;
fastcgi_pass unix:/run/fcgiwrap.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$1;
include fastcgi.conf;
}
}
The idea is, anything under the cgi-bin directory goes through the fastcgi pipe.
Any idea on howI can fix this?
Upon closer inspection, I realized ZoneMinder only has two cgi scripts (nph-zms, zms), so it was easier just to explicitly state them in nginx as so:
# Enable CGI support.
location ~ ^/cgi-bin/(nph-zms|zms)$ {
alias /srv/cgi-bin/zm/;
fastcgi_pass unix:/run/fcgiwrap.sock;
fastcgi_param SCRIPT_FILENAME $document_root/$1;
include fastcgi.conf;
}
Seems to be the best way to go :)

How to setup mass dynamic virtual hosts in nginx?

Been playing with nginx for about an hour trying to setup mass dynamic virtual hosts.
If you ever done it in apache you know what I mean.
Goal is to have dynamic subdomains for few people in the office (more than 50)
Perhaps doing this will get you where you want to be:
server {
root /sites/$http_host;
server_name $http_host;
...
}
I like this as I can literally create sites on the fly, just create new directory named after the domain and point the DNS to the server ip.
You will need some scripting knowledge to put this together. I would use PHP, but if you are good in bash scripting use that. I would do it like this:
First create some folder (/usr/local/etc/nginx/domain.com/).
In main nginx.conf add command : include /usr/local/etc/nginx/domain.com/*.conf;
Every file in this folder should be different vhost names subdomain.conf.
You do not need to restart nginx server for config to take action, you only need to reload it : /usr/local/etc/rc.d/nginx reload
OR you can make only one conf file, where all vhosts should be set. This is probably better so that nginx doesn't need to load up 50 files, but only one....
IF you have problems with scripting, then ask question about that...
Based on user2001260's answer, later edited by partlov, here's my outcome.
Bear in mind this is for a dev server located on a local virtual machine, where the .dev prefix is used at the end of each domain. If you want to remove it, or use something else, the \.dev part in the server_name directive could be edited or altogether removed.
server {
listen 80 default_server;
listen [::]:80 default_server;
# Match any server name with the format [subdomain.[.subdomain...]].domain.tld.dev
server_name ~^(?<subdomain>([\w-]+\.)*)?(?<domain>[\w-]+\.[\w-]+)\.dev$;
# Map by default to (projects_root_path)/(domain.tld)/www;
set $rootdir "/var/www/$domain/www";
# Check if a (projects_root_path)/(subdomain.)(domain.tld)/www directory exists
if (-f "/var/www/$subdomain.$domain/www"){
# in which case, set that directory as the root
set $rootdir "/var/www/$subdomain.$domain/www";
}
root $rootdir;
index index.php index.html index.htm index.nginx-debian.html;
# Front-controller pattern as recommended by the nginx docs
location / {
try_files $uri $uri/ /index.php;
}
# Standard php-fpm based on the default config below this point
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
The regex in server_name captures the variables subdomain and domain. The subdomain part is optional and can be empty. I have set it so that by default, if you have a subdomain, say admin.mysite.com the root is set to the same root as mysite.com. This way, the same front-controller (in my case index.php) can route based on the subdomain. But if you want to keep an altogether different application in a subdomain, you can have a admin.mysite.com dir and it will use that directory for calls to admin.mysite.com.
Careful: The use of if is discouraged in the current nginx version, since it adds extra processing overhead for each request, but it should be fine for use in a dev environment, which is what this configuration is good for. In a production environment, I would recommend not using a mass virtual host configuration and configuring each site separately, for more control and better security.
server_name ~^(?<vhost>[^.]*)\.domain\.com$;
set $rootdir "/var/www/whatever/$vhost";
root $rootdir;
As #Samuurai suggested here is a short version Angular 5 with nginx build integration:
server {
server_name ~^(?<branch>.*)\.staging\.yourdomain\.com$;
access_log /var/log/nginx/branch-access.log;
error_log /var/log/nginx/branch-error.log;
index index.html;
try_files $uri$args $uri$args/ $uri $uri/ /index.html =404;
root /usr/share/nginx/html/www/theft/$branch/dist;
}
Another alternative is to have includes a few levels deep so that directories can be categorized as you see fit. For example:
include sites-enabled/*.conf;
include sites-enabled/*/*.conf;
include sites-enabled/*/*/*.conf;
include sites-enabled/*/*/*/*.conf;
As long as you are comfortable with scripting, it is not very hard to put together some scripts that will quickly set up vhosts in nginx. This slicehost article goes through setting up a couple of vhosts and does it in a way that is easily scriptable and keeps the configurations separate. The only downside is having to restart the server, but that's to be expected with config changes.
Update: If you don't want to do any of the config maintaining yourself, then your only 2 options (the safe ones anyways) would be to either find a program that will let your users manage their own chunk of their nginx config (which will let them create all the subdomains they want), or to create such a user-facing management console yourself.
Doing this yourself would not be too hard, especially if you already have the scripts to do the work of setting things up. The web-based interface can call out to the scripts to do the actual work so that all the web interface has to deal with is managing who has access to what things.

Not found other than homepage

I'm using YII framework. I can access my site through: localhost/index.php
then If I click any links on it it says: 404 not found.
It works on Apache. I'm trying to configure it with NGINX with no success. Can somebody please tell me what can be the problem if something works with Apache but does not work with NGINX?
Log error from nginx:
2011/05/07 11:27:42 [error] 5104#3152: *30 CreateFile() "c:\EWemp\nginx-0.8.52/html/rooms/finished" failed (3: The system cannot find the path specified), client: 127.0.0.1, server: localhost, request: "GET /rooms/finished HTTP/1.1", host: "localhost", referrer: "http://localhost/index.php"
So, I assume that it needs some kind of URL rewrite, since I do not have html/rooms/finished directory.
It is like html/controller/action/ but I do not know what to change in order to get it to work
Yii uses one index.php file to handle all client requests. You need to rewrite /rooms/finished to index.php/rooms/finished.
I have used this Nginx configuration to rewrite all requests to be handled by one index.php file. This configuration uses Fast-CGI to pass PHP requests to PHP-FPM. If you use proxy_pass, you can use rewrite. proxy_pass is explained here.
location / {
index index.php; # Set the index file
try_files $uri $uri/ #handler; # If missing pass the URI to front handler
}
location #handler {
rewrite / /index.php;
}
location ~ .php$ {
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param SCRIPT_FILENAME PATH_TO_SCRIPT$fastcgi_script_name;
fastcgi_pass 127.0.0.1:9000;
}
In my opinion, may be you should make ".htaccess file" like in Apache.