StrongSwan install from source

Download preferred release from download.strongswan.org

  wget https://download.strongswan.org/strongswan.tar.gz

install docs

Untar and cd source directory.

Configure Options

./configure --prefix=/usr --sysconfdir=/etc --enable-systemd --enable-swanctl --enable-openssl --enable-xauth-eap --enable-xauth-noauth
make
make install
Systemd enable/start
systemctl enable strongswan.service
systemctl enable strongswan-starter.service
 
systemctl start strongswan.service
systemctl start strongswan-starter.service

Easy RSA source

git clone https://github.com/ValdikSS/easy-rsa-ipsec.git
cd easy-rsa-ipsec/easyrsa3
./easyrsa init-pki

./easyrsa build-ca nopass

./easyrsa build-server-full domain_name nopass
./easyrsa build-client-full client_name nopass    

./easyrsa export-p12 client_name nopass
Successful export of p12 file. Your exported file is at the following location: /usr/src/easy-rsa-ipsec/easyrsa3/pki/private/<client_name>.p12
cp pki/ca.crt /etc/ipsec.d/cacerts/
cp pki/issued/domain.name.crt /etc/ipsec.d/certs/
cp pki/private/domain.name.key /etc/ipsec.d/private/

Road Warrior config with ipsec.conf

/etc/ipsec.secrets

: RSA domain.name.key
 
<username> : XAUTH "pass"

/etc/ipsec.conf

config setup
        # strictcrlpolicy=yes
        # multiple connections with the one unique cert
	# uniqueids = no
conn %default
	dpdaction=clear
	dpddelay=35s
	dpdtimeout=300s
 
	fragmentation=yes
	rekey=no
 
	ike=aes256gcm16-aes256gcm12-aes128gcm16-aes128gcm12-sha256-sha1-modp2048-modp4096-modp1024,aes256-aes128-sha256-sha1-modp2048-modp4096-modp1024,3des-sha1-modp1024!
 
	esp=aes128gcm12-aes128gcm16-aes256gcm12-aes256gcm16-modp2048-modp4096-modp1024,aes128-aes256-sha1-sha256-modp2048-modp4096-modp1024,aes128-sha1-modp2048,aes128-sha1-modp1024,3des-sha1-modp1024,aes128-aes256-sha1-sha256,aes128-sha1,3des-sha1!
 
	# left - local (server) side
	left=%any
	leftauth=pubkey
	leftcert=domain.name.crt
	leftsendcert=always
	leftsubnet=0.0.0.0/0
 
	# right - remote (client) side
	right=%any
	rightauth=pubkey
	rightsourceip=10.20.30.0/24
	rightdns=8.8.8.8,8.8.4.4
 
conn ikev2-pubkey
	keyexchange=ikev2
	auto=add
 
conn ikev2-pubkey-osx
	also="ikev2-pubkey"
	leftid=domain.name
 
conn ikev1-fakexauth
	keyexchange=ikev1
	rightauth2=xauth-noauth
	auto=add
 
conn ikev2-eap-tls
	also="ikev2-pubkey"
	rightauth=eap-tls
	eap_identity=%identity

restart

systemctl restart strongswan.service
systemctl restart strongswan-starter.service

debug

 swanctl -h 
strongSwan 5.8.1 swanctl
loaded plugins: aes des rc2 sha2 sha1 md5 mgf1 random nonce x509 revocation constraints pubkey pkcs1 pkcs7 pkcs8 pkcs12 pgp dnskey sshkey pem openssl fips-prf gmp curve25519 xcbc cmac hmac
usage:
  swanctl --counters         (-C)  list or reset IKE event counters
  swanctl --initiate         (-i)  initiate a connection
  swanctl --terminate        (-t)  terminate a connection
  swanctl --rekey            (-R)  rekey an SA
  swanctl --redirect         (-d)  redirect an IKE_SA
  swanctl --uninstall        (-u)  uninstall a trap or shunt policy
  swanctl --install          (-p)  install a trap or shunt policy
  swanctl --list-sas         (-l)  list currently active IKE_SAs
  swanctl --monitor-sa       (-m)  monitor for IKE_SA and CHILD_SA changes
  swanctl --list-pols        (-P)  list currently installed policies
  swanctl --list-authorities (-B)  list loaded authority configurations
  swanctl --list-conns       (-L)  list loaded configurations
  swanctl --list-certs       (-x)  list stored certificates
  swanctl --list-pools       (-A)  list loaded pool configurations
  swanctl --list-algs        (-g)  show loaded algorithms
  swanctl --flush-certs      (-f)  flush cached certificates
  swanctl --load-all         (-q)  load credentials, authorities, pools and connections
  swanctl --load-authorities (-b)  (re-)load authority configuration
  swanctl --load-conns       (-c)  (re-)load connection configuration
  swanctl --load-creds       (-s)  (re-)load credentials
  swanctl --load-pools       (-a)  (re-)load pool configuration
  swanctl --log              (-T)  trace logging output
  swanctl --version          (-v)  show version information
  swanctl --stats            (-S)  show daemon stats information
  swanctl --reload-settings  (-r)  reload daemon strongswan.conf
  swanctl --help             (-h)  show usage information

/etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
 iptables -A INPUT -p udp --dport 500 -j ACCEPT
 iptables -A INPUT -p udp --dport 4500 -j ACCEPT
 iptables -A INPUT -p ESP -j ACCEPT
 iptables -A INPUT -p 50 -j ACCEPT
 iptables -A INPUT -p 51 -j ACCEPT
 iptables -I INPUT -m policy --dir in -pol ipsec --proto esp -j ACCEPT
 iptables -I INPUT -m policy --dir in --pol ipsec --proto esp -j ACCEPT
 iptables -I FORWARD -m policy --dir in --pol ipsec --proto esp -j ACCEPT
 iptables -I FORWARD -m policy --dir out --pol ipsec --proto esp -j ACCEPT
 iptables -I OUTPUT -m policy --dir out --pol ipsec --proto esp -j ACCEPT
2019/11/20 · Oleg Zvezdo4kin · 0 комментариев (-я)

Pandoc: html_to_dokuwiki

 pandoc -s -r html https://www.dokuwiki.org/tips:htmltowiki -t dokuwiki > tips.txt

или

 pandoc -f html  -t dokuwiki http://kb.asipto.com/freeswitch:kamailio-3.3.x-freeswitch-1.2.x-sbc > kamailio_fs_sbc.txt
2019/10/02 · Oleg Zvezdo4kin · 0 комментариев (-я)

postgresql pg_dump

Примеры Выгрузка базы данных mydb в файл SQL-скрипта:

 $ pg_dump mydb > db.sql

Восстановление из ранее полученного скрипта в чистую базу newdb:

 $ psql -d newdb -f db.sql

Выгрузка базы данных в формате custom:

 $ pg_dump -Fc mydb > db.dump

Выгрузка базы данных в формате directory:

 $ pg_dump -Fd mydb -f dumpdir

Выгрузка базы данных в формате directory в 5 параллельных потоков:

 $ pg_dump -Fd mydb -j 5 -f dumpdir

Восстановление из архива в чистую новую базу данных newdb:

 $ pg_restore -d newdb db.dump

Выгрузка отдельной таблицы mytab:

 $ pg_dump -t mytab mydb > db.sql

Выгрузка всех таблиц, имена которых начинаются с emp и которые принадлежат схеме detroit, кроме таблицы employee_log:

 $ pg_dump -t 'detroit.emp*' -T detroit.employee_log mydb > db.sql

Выгрузка всех схем, имена которых начинаются с east или west, заканчиваются на gsm и не содержат test:

 $ pg_dump -n 'east*gsm' -n 'west*gsm' -N '*test*' mydb > db.sql

То же самое, но с использованием регулярного выражения:

 $ pg_dump -n '(east|west)*gsm' -N '*test*' mydb > db.sql

Выгрузка всех объектов базы данных, кроме таблиц, имена которых начинаются с ts_:

 $ pg_dump -T 'ts_*' mydb > db.sql

Чтобы указать имя в верхнем или смешанном регистре в ключе -t и связанных с ним, это имя нужно заключить в кавычки; иначе оно будет приведено к нижнему регистру (см. Шаблоны поиска). Но кавычки являются спецсимволом для оболочки, так что и их нужно заключать в кавычки. Так, чтобы выгрузить одну таблицу с именем в смешанном регистре, нужно написать примерно следующее:

 $ pg_dump -t "\"MixedCaseName\"" mydb > mytab.sql
2019/09/24 · Oleg Zvezdo4kin · 0 комментариев (-я)

asterisk mp3 convert to wav 16b 8000hz

Decode mp3 to wav

 lame --decode file.mp3

Convert to 8000 Hz, 16 bit, mono WAV

 sox -v 0.5 file.wav -t wav -b 16 -r 8000 -c 1 file.wav
2019/08/27 · Oleg Zvezdo4kin · 0 комментариев (-я)

openresty

Introduction

OpenResty is a web server which extends Nginx by bundling it with many useful Nginx modules and Lua libraries. OpenResty excels at scaling web applications and services. For example, one module it includes enables you to write Lua code which will execute directly in an Nginx worker, enabling high-performance applications.

In this guide, you will set up OpenResty from source; the pre-built packages for some distros can be out of date. You'll also explore some simple example applications with OpenResty's unique features.

To follow this guide, you will need:

Note that Nginx should not be installed. It's included in OpenResty and having it installed ahead of time will conflict.

In this section, we will install OpenResty from source.

First, find the latest OpenResty source code release from the Download page on the OpenResty website. Download the tarball, making sure to replace the version number by the latest version if it has changed.

wget https://openresty.org/download/openresty-1.11.2.2.tar.gz

Download the PGP key file as well so we can verify the contents of the file.

wget https://openresty.org/download/openresty-1.11.2.2.tar.gz.asc

Next, we need to add the public key of the author as listed on the download page. At time of writing, this is public key A0E98066. However, do check if it has changed; it is listed on the same downloads page.

gpg --keyserver pgpkeys.mit.edu --recv-key A0E98066

You should see the following output (with your username in place of sammy):

Outputgpg: directory `/home/sammy/.gnupg' created
gpg: new configuration file `/home/sammy/.gnupg/gpg.conf' created
gpg: WARNING: options in `/home/sammy/.gnupg/gpg.conf' are not yet active during this run
gpg: keyring `/home/sammy/.gnupg/secring.gpg' created
gpg: keyring `/home/sammy/.gnupg/pubring.gpg' created
gpg: requesting key A0E98066 from hkp server pgpkeys.mit.edu
gpg: /home/sammy/.gnupg/trustdb.gpg: trustdb created
gpg: key A0E98066: public key "Yichun Zhang (agentzh) <agentzh@gmail.com>" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)

Check that the name on the public key (in this case, it's «Yichun Zhang») matches the name listed on the OpenResty website.

Now, check whether the signature file matches the downloaded .tar.gz file.

gpg openresty-1.11.2.2.tar.gz.asc

You'll see the following output:

Outputgpg: assuming signed data in `openresty-1.11.2.2.tar.gz'
gpg: Signature made Thu 17 Nov 2016 10:24:29 PM UTC using RSA key ID A0E98066
gpg: Good signature from "Yichun Zhang (agentzh) <agentzh@gmail.com>"
gpg: WARNING: This key is not certified with a trusted signature!
gpg:          There is no indication that the signature belongs to the owner.
Primary key fingerprint: 2545 1EB0 8846 0026 195B  D62C B550 E09E A0E9 8066

The warning you see is because you haven't personally verified whether this key belongs to the owner (i.e., you have not signed the public key with your own private key). There isn't an easy way to completely guarantee that this public key belongs to the owner, which it isn't completely trusted.

However, in this case, Good signature indicates that this file is indeed the file that the authors of OpenResty intended to distribute, so we can go ahead with the installation.

Next, extract the downloaded file and move into the newly created directory.

tar -xvf openresty-1.11.2.2.tar.gz
cd openresty-1.11.2.2

We will need to install the necessary tools to compile OpenResty. For more information about compiling programs from source, see this tutorial about using make to install packages from source.

sudo apt-get install build-essential

We will also need to install some other packages:

  • readline: This will be used by OpenResty for the command line interface.
  • ncurses: This is another piece of software that will be used by OpenResty for its command line interface.
  • PCRE: This software will provide OpenResty with regular expression capabilities.
  • OpenSSL: OpenSSL is used for secure communication, such as TLS (HTTPS).
  • Perl: Perl is a programming language that can be used in OpenResty.

To install these packages, execute the following command:

sudo apt-get install libreadline-dev libncurses5-dev libpcre3-dev libssl-dev perl

We now have all the components necessary to build and install OpenResty.

We will configure OpenResty with PCRE regular expression and IPv6 support. We will also parallelize part of the building process by supplying the -j2 flag, which will tell make that 2 jobs can be run simultaneously. This command will mostly test if all dependencies are available on your system and gather information that will be used by the build step later on. It will also already build some dependencies, such as LuaJIT.

./configure -j2 --with-pcre-jit --with-ipv6

Then you can build OpenResty, again by supplying the -j2 flags for parallelism. This will compile OpenResty itself.

make -j2

Finally, you can install OpenResty. Using sudo makes sure all files can be copied to the correct locations on the system so that OpenResty can find them when it is running.

sudo make install

You will need to allow HTTP connections in your firewall for the web server to work.

sudo ufw allow http

You can optionally also allow HTTPS with sudo ufw allow https if you are going to be using it. You can verify the change in the firewall by checking its status.

sudo ufw status

You should see HTTP traffic (port 80) allowed in the displayed output, as well as HTTPS (port 443) if you added it.

OutputStatus: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
80                         ALLOW       Anywhere
443                        ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
80 (v6)                    ALLOW       Anywhere (v6)
443 (v6)                   ALLOW       Anywhere (v6)

You can now check whether the installation has worked. First, start OpenResty.

sudo /usr/local/openresty/bin/openresty

It will complete immediately without text output if the command is successful. In that case, you can visit http://your_server_ip in your browser. You'll see a page which says Welcome to OpenResty! with confirmation that it's fully installed and working.

You can now stop the OpenResty server.

sudo /usr/local/openresty/bin/openresty -s quit

OpenResty is installed, but you still need to configure OpenResty to run on startup so the server does not have to be started manually.

Here, we are going to set up OpenResty as a service so it starts automatically on boot. We will do this using the systemd init service. You can read this systemd basics tutorial for more information, and this unit file tutorial for information on unit files specifically.

Start by creating a new systemd file with nano or your favorite text editor.

sudo nano /etc/systemd/system/openresty.service

For this tutorial, we'll copy the default Nginx systemd file from a fresh installation and modify it for OpenResty. The complete file looks like this and should be pasted into the file we just opened. We'll walk through each part of the file to explain what it's doing.

/etc/systemd/system/openresty.service

# Stop dance for OpenResty
# A modification of the Nginx systemd script
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the Nginx process.
# If, after 5s (--retry QUIT/5) OpenResty is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if OpenResty is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# Nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A dynamic web platform based on Nginx and LuaJIT.
After=network.target

[Service]
Type=forking
PIDFile=/run/openresty.pid
ExecStartPre=/usr/local/openresty/bin/openresty -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;'
ExecReload=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/openresty.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

In the [Unit] Section:

  • After=network.target makes OpenResty start after the network is up so that OpenResty can bind and listen to ports. This allows it to be reached from the outside.

In the [Service] section:

  • Type=forking tells systemd that the process we call in ExecStart will start the service in the background and that the process will stop itself after it has done so.
  • PIDFile=/run/openresty.pid tells systemd where to find the PID file OpenResty creates when it is started. This allows systemd to know whether OpenResty is still running.
  • ExecStartPre=/usr/local/openresty/bin/openresty -t -q -g 'daemon on; master_process on;' calls the OpenResty script without starting it. The -t flag tells OpenResty we only want it to test the configuration file; the -q flag tells it that we want to suppress any non-error output; the -g flag sets the global directives daemon on; master_process on that tell OpenResty we want it to start in the background as a daemon. We execute this script as ExecStartPre so that systemd will not try starting OpenResty when the configuration file is invalid, as it will error out on this command.
  • ExecStart=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;' actually starts OpenReesty. This is the same as ExecStartPre without the -t flag.
  • ExecReload=/usr/local/openresty/bin/openresty -g 'daemon on; master_process on;' -s reload tells systemd to run this command when we run systemctl reload openresty. The -s flag tells OpenResty to reload its configuration file.
  • ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/openresty.pid tells systemd to run this command when OpenResty is stopped. It sents SIGSTOP to the process listed in the PID file. If it's still running 5 seconds later, systemd will take control via the following two options.
  • TimeoutStopSec=5 tells systemd we want the process stopped in 5 seconds. If it does not stop, systemd will forcibly try stopping OpenRest.
  • KillMode=mixed specifies how systemd should try stopping OpenResty when it has not stopped after 5 seconds.

In the [Install] section:

  • WantedBy=multi-user.target tells systemd when we want the service to be started if it is configured to be started at boot. multi-user.target means the service will only be started when a multi-user system has been started, i.e. we can run OpenResty as a different user.

That's all for the etc/systemd/system/openresty.service file. Next, we need to customize the OpenResty Nginx configuration file and enable the service.

Open the configuration file first.

sudo nano /usr/local/openresty/nginx/conf/nginx.conf

By default, it will look like this:

Default /usr/local/openresty/nginx/conf/nginx.conf

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

. . .

Delete everything before the events { line, and replace it with the following three lines:

Updated /usr/local/openresty/nginx/conf/nginx.conf

user www-data;
worker_processes  auto;
pid /run/openresty.pid;

events {
    worker_connections  1024;
}

. . .

This file will make sure we are running as the www-data user and that systemd can recognize when OpenResty is running due to the pid line that will be created by OpenResty once it starts.

Save and close the file.

Next, create the log directory.

sudo mkdir /var/log/openresty

Reload the systemd service so that it can find our file.

sudo systemctl daemon-reload

Now, start OpenResty via systemd.

sudo systemctl start openresty

You can now visit http://your_server_ip again and see the same web page as before. The difference is that now, the process has been started by systemd.

The last step is to enable the service which will make sure that OpenResty is started on boot.

sudo systemctl enable openresty

You can learn more about managing systemd services and units in our services and units tutorial.

Now that we have configured the service, we can further configure OpenResty so it will e.g. log to a common location.

To configure OpenResty, we have used the default Nginx configuration as a reference, so that it will mostly match what you might be familiar with.

First, open the OpenResty configuration file again:

sudo nano /usr/local/openresty/nginx/conf/nginx.conf

This time, we are going to modify the http block and move the server block inside this http block to a new file to have a better structure. First, locate the http { line, and delete everything after it, except for the final line with the corresponding }.

Current /usr/local/openresty/nginx/conf/nginx.conf

user www-data;
worker_processes  auto;
pid /run/openresty.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    . . .
}

Then, copy the following into the http block so that your entire file looks like this. We'll go over the changes one at a time.

/usr/local/openresty/nginx/conf/nginx.conf

user www-data;
worker_processes  auto;
pid /run/openresty.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;

    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;

    keepalive_timeout  65;

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    access_log /var/log/openresty/access.log;
    error_log /var/log/openresty/error.log;

    gzip  on;
    gzip_disable "msie6";

    include ../sites/*;
}

Save and close the file.

The changes we made to the default file are:

  • Uncommenting tcp_nopush on;, which tells OpenResty to send only full packets. This option is useful when using the sendfile option, which will allow OpenResty to optimize sending static files to a client.
  • Adding tcp_nodelay on;. This option will try sending packets as soon as possible, which may seem contrary to the above option, but it is used at a different time. tcp_nodelay is only used when using the keepalive option on HTTP requests, which is a connection to a web server by a web browser that will avoid the overhead of initiating an HTTP connection every time a request is made.
  • Adding and modifying the ssl_protocols and ssl_prefer_server_ciphers lines. These options configure the SSL options of OpenResty. We have removed old protocols that are vulnerable to known attacks on HTTPS, such as the POODLE attack.
  • Adding the access_log and error_log lines, which configures where the logs of the web server. We store the logs at the /var/log/openresty directory, which we created in the previous step.
  • Uncommenting gzip on and adding gzip_disable "msie6". These options will configure GZIP, which will compress web pages so that there is less data to transfer. We also add the last option because Internet Explorer 6 (and older) does not always process GZIP content properly.
  • Adding include ../sites/*;, which tells OpenResty to look for extra configuration files in the /usr/local/openresty/nginx/sites directory, which we will be created in a moment.
  • Removing all server blocks, which we'll relocate to a new file later in this step.

Next, create the new sites directory that we specified in the include line.

sudo mkdir /usr/local/openresty/nginx/sites

Create the default site.

sudo nano /usr/local/openresty/nginx/sites/default.conf

Add the following in this new file. This is the relocation of the original server block from nginx.conf, but has inline comments for more detail.

/usr/local/openresty/nginx/sites/default.conf

server {
    # Listen on port 80.
    listen 80 default_server;
    listen [::]:80 default_server;

    # The document root.
    root /usr/local/openresty/nginx/html/default;

    # Add index.php if you are using PHP.
    index index.html index.htm;

    # The server name, which isn't relevant in this case, because we only have one.
    server_name _;

    # When we try to access this site...
    location / {
        # ... first attempt to serve request as file, then as a directory,
        # then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

    # Redirect server error pages to the static page /50x.html.
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root /usr/local/openresty/nginx/html;
    }
}

Save and close the file.

Now, create a new directory for this site.

sudo mkdir /usr/local/openresty/nginx/html/default

Then move the original index.html from its original location to the new directory.

sudo mv /usr/local/openresty/nginx/html/index.html /usr/local/openresty/nginx/html/default

Finally, restart OpenResty to use this new site.

sudo systemctl restart openresty

You can now again visit http://your_server_ip and see the same web page as before.

Now that OpenResty is fully configured, we can try some of the features introduced by OpenResty that are not available in Nginx by default.

In this section, we will look at a combination of different modules added by OpenResty which all exist to accommodate Lua scripting. We will modifying /usr/local/openresty/nginx/sites/default.conf throughout this step, so open it first.

sudo nano /usr/local/openresty/nginx/sites/default.conf

First, we are going to look at the content_by_lua_block configuration option. Copy the location block from the example configuration below and add it into the server block, below the two existing location blocks.

/usr/local/openresty/nginx/sites/default.conf content_by_lua_block example

server {
    . . .

    location /example {
         default_type 'text/plain';

         content_by_lua_block {
             ngx.say('Hello, Sammy!')
         }
    }
}

Save and close the file, then reload the configuration.

sudo systemctl reload openresty

If you visit http://your_server_ip/example now, you'll see a page which says Hello, Sammy!. Let's explain how this works.

The content_by_lua_block configuration directive executes everything within it as Lua code. Here, we used the Lua function ngx.say to print the message Hello, Sammy! to the page.

For another example, replace the contents of the location /example block with this:

/usr/local/openresty/nginx/sites/default.conf content_by_lua_file example

server {
    . . .

    location /example {
         default_type 'text/plain';

         content_by_lua_file /usr/local/openresty/nginx/html/default/index.lua;
    }
}

The content_by_lua_file loads the Lua content from an external file, so let's create the one we specified above: /usr/local/openresty/nginx/html/default/index.lua.

sudo nano /usr/local/openresty/nginx/html/default/index.lua

Add the following to the file, then save and close it.

/usr/local/openresty/nginx/html/default/index.lua

local name = ngx.var.arg_name or "Anonymous"
ngx.say("Hello, ", name, "!")

This is a simple piece of Lua which reads a query parameter in the URL, name, and customizes the greeting message. If no parameter is passed, it uses «Anonymous» instead.

Reload the configuration again.

sudo systemctl reload openresty

Now, visit http://your_server_ip/example?name=Sammy in your browser. This will display Hello, Sammy!. You can change the name query parameter, or omit it entirely.

Hello, Sammy!

You can also change the name query parameter to show any other name.

Warning: Do not place the Lua file you are loading in an accessible location from the web. If you do, your application code might be comprised if someone accesses this file. Place the file outside of your document root, for example by changing the document root to /usr/local/openresty/nginx/html/default/public and placing the Lua files one directory above it.

In this article you set up OpenResty, which will enable you to use Lua scripts in an Nginx worker. It is possible to create much more complex Lua scripts. You can also, for example, restrict access using Lua scripts or rewrite certain requests using Lua. You can find the documentation on the lua-nginx-module's GitHub page. There are even complete web frameworks which use Lua on OpenResty, such as Lapis.

If you want to learn more, you can visit the OpenResty website. Because OpenResty is just an extended Nginx installation, you can also learn how to set up server blocks in the Nginx server blocks tutorial, but make sure to replace the paths used in that tutorial by the paths used in this one.

2019/06/26 · Oleg Zvezdo4kin · 0 комментариев (-я)

<< Новые записи | Предыдущие записи >>

page
start
Asterisk Call Center Stats Compare Answered Unanswered Calls Example
AJAX PHP MySQL CSV
apache_httpd_reverse_proxy
Asterisk AMI AJAM Parser php
Asterisk round-robin memory trunk group
asterisk mp3 convert to wav 16b 8000hz
autovacuum
Baresip
Ротация bash_history
bash manipulating string
blind
Checking the speed of writing to a disc
Create boot usb stick
CURL GOIP SMS
Debian 9 officially FS
Debian 9 php5.6 Install
debian 9 VPN pptp
Debian9 Stretch Configure Locale en_US.UTF-8
Asterisk Advanced Message Queue Protocol
Динамические массивы в C
Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
freeswitch a1-hash and password generating
FreeSwitch Directory MySql storage with Lua Dbh
Debian 9 Stretch Install Goip Sms Server
Handlebars Helper LastCall Queue Agent
Pandoc: html_to_dokuwiki
Intraservice API + Asterisk + cURL
Lua as a Configuration And Data Exchange Language For "C"
How non-root users manage systemd
openresty
postgres alter sequence
postgresql pg_dump
Reboot IP LDK over telnet from bash script
recursively-chmod-all-directories-except-files
Redis+Lua - routing calls by mobile operator DEF code
Bash: Поиск и операции с файлами по выборке имен из БД
Minimum configuration requirements for using stun server (Coturn)
Slack Incoming Webhook with Php Curl
sngrep
switch_core_db.c:92 SQLite is BUSY
StrongSwan install from source
Sublime Regex
TCPdump: программа для перехвата и анализа SIP-сообщений.
TDLib send message by phone number
ubuntu_remap_super_key
Обновить/Вставить значение в jsonb поле по ключу ( Postgresql )
Vim Commands Cheat Sheet
  • blog.txt
  • Последние изменения: 2019/03/27