OPNsense Forum

Archive => 19.7 Legacy Series => Topic started by: sharif779 on June 11, 2019, 12:50:52 pm

Title: Convert lighttpd to nginx in Opnsense
Post by: sharif779 on June 11, 2019, 12:50:52 pm
I want to change lighttpd to nginx as default web server. I have already installed and changed configuration in nginx server like lighttpd but api and ui routing is not working.
Here is my changed code for nginx configuration
Code: [Select]
server {
listen 80;

server_name _;

root /usr/local/www;
index index.php index.html;
        location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
       
        location /ui/{
    alias /usr/local/opnsense/www/;
    try_files $uri $uri/ /ui//ui/index.php?_url=$uri&$args;
    location ~ /ui/.+\.php$ {
                include fastcgi_params;
                fastcgi_buffers 256 4k;
                fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            }
        }

location /api/{
    alias /usr/local/opnsense/www/;
    try_files $uri $uri/ /api//api/api.php?_url=$uri&$args;
    location ~ /api/.+\.php$ {
                include fastcgi_params;
                fastcgi_buffers 256 4k;
                fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
            }
        }

        location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
    }
}
   
After changing config api response is
Code: [Select]
{
    "message": "controller OPNsense\\Core\\Api\\IndexController not found",
    "status": 400
}
I have added IndexController though this controller was not in opnsense but it is not working at all.
Can you please help.It is needed to change web server .
Title: Re: Convert lighttpd to nginx in Opnsense
Post by: fabian on June 11, 2019, 11:33:41 pm
The configuration is already there, it is just officially not (yet) supported. Open the nginx.conf and you will find an include for a ready to use web interface config which is commented out. Just remove the comment character. Kill lighttpd and start nginx via CLI using the service tool or the rc script directly.
Title: Re: Convert lighttpd to nginx in Opnsense
Post by: sharif779 on June 12, 2019, 07:15:17 am
I am using opnsense 19.1 and there is no nginx.conf in opnsense src. But I found in opnsense git source .
I have added following code from https://github.com/opnsense/plugins/blob/master/www/nginx/src/opnsense/service/templates/OPNsense/Nginx/webgui.conf (https://github.com/opnsense/plugins/blob/master/www/nginx/src/opnsense/service/templates/OPNsense/Nginx/webgui.conf)
Code: [Select]
location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php-webgui.socket;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index  index.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    include        fastcgi_params;
  }

  # Phalcon ui and api routing

  location @apirequest {
    root /usr/local/opnsense/www;
    include        fastcgi_params;
    fastcgi_param  QUERY_STRING &$query_string;
    fastcgi_param  SCRIPT_FILENAME /usr/local/opnsense/www/api.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_intercept_errors off;
    fastcgi_pass   unix:/var/run/php-webgui.socket;
  }
  location @guirequest {
    root /usr/local/opnsense/www;
    include        fastcgi_params;
    fastcgi_param  QUERY_STRING $query_string;
    fastcgi_param  SCRIPT_FILENAME /usr/local/opnsense/www/index.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_intercept_errors off;
    fastcgi_pass   unix:/var/run/php-webgui.socket;
  }
  location ~ ^/ui/(?<path>[^\?]+)(?<query>\?(.*))? {
    root /usr/local/opnsense/www;
    try_files /$path @guirequest;
  }
  location ~ ^/api/(?<path>[^\?]+)(?<query>\?(.*))?{
    root /usr/local/opnsense/www;
    try_files /$path @apirequest;
  }
but same problem exists.
I access http://192.168.30.189/api/iop/dhcp/list (http://192.168.30.189/api/iop/dhcp/list) this location but reply is
Code: [Select]
{
    "message": "controller OPNsense\\Core\\Api\\IndexController not found",
    "status": 400
}
Can you please tell me the location of nginx.conf.
Here is my nginx configuration.
Code: [Select]
server {
listen 80;

server_name _;

root /usr/local/www;
#compress.cache-dir = "/tmp/lighttpdcompress/"
  gzip_types text/plain text/css text/xml text/javascript;

  #server.upload-dirs = ( "/root/", "/tmp/", "/var/" )
  # server.max-request-size    = 2097152

  expires 50h;

  # Maximum idle time with nothing being written (php downloading)
  #fastcgi_read_timeout = 999

  ## where to send error/access-messages to
  access_log syslog:server=127.0.0.1,facility=daemon;
  access_log /var/log/nginx/webgui.access.log;
  error_log syslog:server=127.0.0.1,facility=daemon;
  error_log /var/log/nginx/error.log debug;

index index.php index.html index.htm default.htm;
       # mimetype mapping
  types {
    application/x-ns-proxy-autoconfig pad.dat;
    application/pdf pdf;
    application/pgp-signature sig;
    application/futuresplash spl;
    application/octet-stream class;
    application/postscript ps;
    application/x-bittorrent torrent;
    application/x-dvi dvi;
    application/x-gzip gz;
    application/x-ns-proxy-autoconfig pac;
    application/x-shockwave-flash swf;
    application/x-tgz tar.gz tgz;
    application/x-tar tar;
    application/zip zip;
    audio/mpeg mp3;
    audio/x-mpegurl m3u;
    audio/x-ms-wma wma;
    audio/x-ms-wax wax;
    audio/x-wav ogg;
    audio/x-wav wav;
    image/gif gif;
    image/jpeg jpg jpeg;
    image/png png;
    image/svg+xml svg;
    image/x-xbitmap xbm;
    image/x-xpixmap xpm;
    image/x-xwindowdump xwd;
    text/css css;
    text/html html htm;
    text/javascript js;
    text/plain asc;
    text/plain c;
    text/plain conf;
    text/plain text txt;
    text/xml dtd;
    text/xml xml;
    video/mpeg mpeg;
    video/mpeg mpg;
    video/quicktime mov qt;
    video/x-msvideo avi;
    video/x-ms-asf asf asx;
    video/x-ms-wmv wmv;
    application/x-bzip bz2;
    application/x-bzip-compressed-tar tbz tar.bz2;
  }

       
        location ~ \.php$ {
    fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_index  index.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    fastcgi_intercept_errors on;
    include        fastcgi_params;
  }

  # Phalcon ui and api routing

  location @apirequest {
    root /usr/local/opnsense/www;
    include        fastcgi_params;
    fastcgi_param  QUERY_STRING &$query_string;
    fastcgi_param  SCRIPT_FILENAME /usr/local/opnsense/www/api.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_intercept_errors off;
    fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
  }
  location @guirequest {
    root /usr/local/opnsense/www;
    include        fastcgi_params;
    fastcgi_param  QUERY_STRING $query_string;
    fastcgi_param  SCRIPT_FILENAME /usr/local/opnsense/www/index.php;
    fastcgi_param TLS-Cipher $ssl_cipher;
    fastcgi_param TLS-Protocol $ssl_protocol;
    fastcgi_param TLS-SNI-Host $ssl_server_name;
    fastcgi_intercept_errors off;
    fastcgi_pass   unix:/var/run/php/php7.0-fpm.sock;
  }
  location ~ ^/ui/(?<path>[^\?]+)(?<query>\?(.*))? {
    root /usr/local/opnsense/www;
    try_files /$path @guirequest;
  }
  location ~ ^/api/(?<path>[^\?]+)(?<query>\?(.*))?{
    root /usr/local/opnsense/www;
    try_files /$path @apirequest;
  }
}
FYI , I am using Opnsense in debian
Title: Re: Convert lighttpd to nginx in Opnsense
Post by: fabian on June 12, 2019, 08:17:18 pm
Is your PHP correctly configured? It looks like your class is requesting the wrong controller. IOP does not exist in OPNsense...
Title: Re: Convert lighttpd to nginx in Opnsense
Post by: sharif.hossain on June 12, 2019, 08:28:18 pm
IOP controller exists in opnsense.I have added it.When I switched to lighttpd it’s working fine.