「 流行りの NginX をフロントエンドに、 NodeJS をバックエンドにしたやつを
立てましょう」
「 Team Viewer を使って 隣のPCにリモート・ログイン☆
Windows(10)の上で Linux(CentOS) を走らせて リモート・ログオフ☆
PuTTy で Linux にログインだぜ☆」
「 まず NodeJS を走らすかだぜ☆
NodeJS は pm2 で走らせよう☆
コンソールウィンドウの横幅を贅沢に広げて……☆」
pm2 -h
pm2 list
clear
node -h
node
> console.log("hello")
hello
undefined
Node.jsで5行Webサーバを書いてみよう〈Node.jsシリーズ vol.1〉
sudo su -
cd /
dir
cd var/local
dir
cd /root
dir
cd /home
dir
whereis node
node: /usr/bin/node /usr/share/man/man1/node.1.gz
cd /etc
dir
「 NodeJSの設定ファイルは etc ディレクトリに無いぜ☆」
「 NodeJS の listenメソッドが書いた .jsファイルが どこかにあるだろ☆」
「 ダメだ進まん☆ 一旦 Windows10 で NodeJS のサーバーを構築し、
Linuxは ファイルを置くだけにしよう☆」
「 [Windows]-[R], cmd
でコンソール・ウィンドウ起動☆」
node -h
'node' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。
「 Cドライブ直下に node-js という名前のフォルダーを作りましょう」
node -h
'node' は、内部コマンドまたは外部コマンド、
操作可能なプログラムまたはバッチ ファイルとして認識されていません。
「 コンソール・ウィンドウを閉じて もう一度 [Windows]-[R], cmd
☆
よしいけた☆」
node
> console.log("hehehe")
hehehe
undefined
「 あっ、本番環境とテスト環境で 設定分けたいぜ めんどくせ☆」
「 Java Script なら JSON を使えばいいのでは……☆?」
conf.json
{
"port":80,
"ip_address":"127.0.0.1"
}
start-server.js
const fs = require('fs');
const conf = JSON.parse(fs.readFileSync('./conf.json', 'utf8'));
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Kifuwarabe!\n');
}).listen(conf.port, conf.ip_address);
> cd c:\muzudho\node
> node start-server.js
「 ブラウザで http://127.0.0.1:80/ にアクセスしろだぜ☆」
Hello Kifuwarabe!
「 Nginx をダウンロードしたが、なんで こういうソフトって 設定ファイルと 実行ファイルを分けれるように
してくれないんだろな☆ 最小限の部分だけ バックアップ取りたいのに……☆」
「 コマンド・プロンプトを右クリックして 管理者権限で実行☆」
cd C:\muzudho\nginx-1.16.0
start nginx.exe
Hello Kifuwarabe!
「 NodeJSを動かしているコンソール・ウィンドウで [Ctrl]+[C]
、ブラウザを F5
リロード☆」
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and working. Further configuration is required.
For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.
Thank you for using nginx.
「 静的ファイルはNginxが、動的ファイルは NodeJS が担当するように
Nginx のリバース・プロキシ機能を利用して バックエンド・ノードに NodeJS を指定しましょう」
Nginx を Node.js のリバースプロキシとして使う
「 Nginxの設定ファイルはどれだぜ☆? conf/nginx.conf だろうか☆?
コメントがいっぱい書いていて 読みにくいな……☆」
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
「 どこに upstream の設定を書けばいいんだぜ☆? 分からないときは 適当に書くか☆」
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# backend_node に LoopBack サーバー(localhost:3000)を追加
upstream backend_node {
ip_hash;
server 127.0.0.1:3000;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
# 存在するファイルパスが指定された場合はそのまま Nginx で処理
if ( -f $request_filename ) {
break;
}
# 存在しないファイルパスが指定された場合は backend_node で処理
if ( !-f $request_filename ) {
proxy_pass http://backend_node;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
「 NodeJSの方はポート 3000 で動くように変更しましょう。
Nginxの方は ポート80 のままで」
「 NodeJSの conf.json のポート番号を 3000 に書き換えて コンソール・ウィンドウから再起動☆
Google chrome で localhost:80
では Nginx が、 localhost:3000
では NodeJS が動いている☆」
nginx -s quit
localhost:80
このサイトにアクセスできません
start nginx.exe
localhost:80
Hello Kifuwarabe!
「 よし、バックエンド・ノードの NodeJS が優先されたぜ☆」
An error occurred.
Sorry, the page you are looking for is currently unavailable.
Please try again later.
If you are the system administrator of this resource then you should check the error log for details.
Faithfully yours, nginx.
「 今回作ったファイルを Linux に持っていって、同じことをしましょう」
「 /home/muzudho/nodejs
ディレクトリを作るぜ☆」
「 PuTTy の pscp.exe を使ってファイル転送するの めんどくさい……☆」
「 FileZillaで SFTP転送でけた……☆
IPアドレスを書き換えて……☆」
> node start-server.js
events.js:174
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use xxx.xxx.xxx.xxx:3000
curl localhost:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>新しいメールが /var/spool/mail/root にあります
「 だったら Nginx の conf/nginx.conf を設定すれば終わりかだぜ☆」
# cd /etc/nginx
# ls
# vi nginx.conf
# nginx - v
nginx: invalid option: "v"
# nginx -V
nginx version: nginx/1.15.9
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
「 Nginxのローカルのリポジトリは前に どこかに作っただろ☆」
CentOS 7 (5, 6) で "安定版 (最新版)" のNginxをインストールする方法
# cd /etc/yum.repos.d/nginx.repo
-bash: cd: /etc/yum.repos.d/nginx.repo: ディレクトリではありません
新しいメールが /var/spool/mail/root にあります
# cd /etc/yum.repos.d
# dir
# vi nginx repo
"nginx" [New File]
:q!
# vi nginx.repo
nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
:q!
cd ~
sudo yum -y --enablerepo=nginx install nginx
読み込んだプラグイン:fastestmirror, langpacks
Repository nodesource is listed more than once in the configuration
Repository nodesource-source is listed more than once in the configuration
Error getting repository data for nginx, repository not found
Nginxのインストールとアンインストール (CentOS)
# service nginx stop
Redirecting to /bin/systemctl stop nginx.service
新しいメールが /var/spool/mail/root にあります
# chkconfig nginx off
情報:'systemctl disable nginx.service'へ転送しています。
Removed symlink /etc/systemd/system/multi-user.target.wants/nginx.service.
# yum remove nginx
# yum remove nginx
読み込んだプラグイン:fastestmirror, langpacks
Repository nodesource is listed more than once in the configuration
Repository nodesource-source is listed more than once in the configuration
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nginx.x86_64 1:1.15.9-1.el7_4.ngx を 削除
--> 依存性解決を終了しました。
base/7/x86_64 | 3.6 kB 00:00:00
epel/x86_64/metalink | 5.4 kB 00:00:00
epel/x86_64 | 5.3 kB 00:00:00
epel/x86_64/updateinfo | 977 kB 00:00:06
epel/x86_64/primary_db | 6.7 MB 00:00:25
extras/7/x86_64 | 3.4 kB 00:00:00
extras/7/x86_64/primary_db | 200 kB 00:00:01
nginx-mainline/7/x86_64 | 2.9 kB 00:00:00
nginx-mainline/7/x86_64/primary_db | 144 kB 00:00:24
nginx-stable/7/x86_64 | 2.9 kB 00:00:04
nginx-stable/7/x86_64/primary_db | 46 kB 00:00:06
nodesource/x86_64 | 2.5 kB 00:00:00
nodesource/x86_64/primary_db | 60 kB 00:00:00
updates/7/x86_64 | 3.4 kB 00:00:00
updates/7/x86_64/primary_db | 5.0 MB 00:00:09
依存性を解決しました
======================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
======================================================================================================================================================================================================================
削除中:
nginx x86_64 1:1.15.9-1.el7_4.ngx @nginx-mainline 2.7 M
トランザクションの要約
======================================================================================================================================================================================================================
削除 1 パッケージ
インストール容量: 2.7 M
「 Zzz☆(^~^) 2時間ぐらい 眠ってしまった☆(^~^) おっ、消されているな☆」
上記の処理を行います。よろしいでしょうか? [y/N]
y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
削除中 : 1:nginx-1.15.9-1.el7_4.ngx.x86_64 1/1
警告: /etc/nginx/nginx.conf は /etc/nginx/nginx.conf.rpmsave として保存されました。
警告: /etc/nginx/conf.d/default.conf は /etc/nginx/conf.d/default.conf.rpmsave として保存されました。
検証中 : 1:nginx-1.15.9-1.el7_4.ngx.x86_64 1/1
削除しました:
nginx.x86_64 1:1.15.9-1.el7_4.ngx
完了しました!
新しいメールが /var/spool/mail/root にあります
# sudo vi /etc/yum.repos.d/nginx.repo
nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
「 このファイルは消えてないのか☆ これ使えないのかだぜ☆?」
:wq
# sudo yum install nginx
読み込んだプラグイン:fastestmirror, langpacks
Repository nodesource is listed more than once in the configuration
Repository nodesource-source is listed more than once in the configuration
Loading mirror speeds from cached hostfile
* base: ftp-srv2.kddilabs.jp
* epel: ftp.yz.yamagata-u.ac.jp
* extras: ftp-srv2.kddilabs.jp
* updates: ftp-srv2.kddilabs.jp
nginx-mainline | 2.9 kB 00:00:00
nginx-stable | 2.9 kB 00:00:02
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ nginx.x86_64 1:1.17.0-1.el7.ngx を インストール
--> 依存性解決を終了しました。
依存性を解決しました
======================================================================================================================================================================================================================
Package アーキテクチャー バージョン リポジトリー 容量
======================================================================================================================================================================================================================
インストール中:
nginx x86_64 1:1.17.0-1.el7.ngx nginx-mainline 767 k
トランザクションの要約
======================================================================================================================================================================================================================
インストール 1 パッケージ
総ダウンロード容量: 767 k
インストール容量: 2.7 M
Is this ok [y/d/N]:
y
Downloading packages:
nginx-1.17.0-1.el7.ngx.x86_64.rpm | 767 kB 00:00:47
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
インストール中 : 1:nginx-1.17.0-1.el7.ngx.x86_64 1/1
----------------------------------------------------------------------
Thanks for using nginx!
Please find the official documentation for nginx here:
* http://nginx.org/en/docs/
Please subscribe to nginx-announce mailing list to get
the most important news about nginx:
* http://nginx.org/en/support.html
Commercial subscriptions for nginx are available on:
* http://nginx.com/products/
----------------------------------------------------------------------
検証中 : 1:nginx-1.17.0-1.el7.ngx.x86_64 1/1
インストール:
nginx.x86_64 1:1.17.0-1.el7.ngx
完了しました!
新しいメールが /var/spool/mail/root にあります
# nginx -V
nginx version: nginx/1.17.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
「 一応バージョン番号は上がった☆ メインライン(現行版)と同じだぜ☆」
「 Windows では start nginx.exe
だったが、Linuxで .exe
は無いだろう☆ 調べようぜ☆」
「 最近は service
より systemctl
の方が流行りじゃないかだぜ☆?」
# systemctl start nginx
新しいメールが /var/spool/mail/root にあります
# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
「 状況を整理しよう☆
localhost:3000
では NodeJS/Express が、
localhost:80
では Nginx が動いているな☆?」
「 恐らく去年のわたしが動かしていた NodeJS/Express だけどな☆ NodeJSまでアンインストールするのはめんどくさい☆」
「 Nginx のバックエンド・ノードに NodeJS を指定して、
localhost:80
から NodeJS/Express につながるようにしてくれだぜ☆」
「 今日の晩飯 こんなんだぜ☆ お前ら プログラマーになりたい☆?」
「 美味そうだな☆ それより nginx.conf を開けだぜ☆」
# cd /etc/nginx
# dir
# vi nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
「 Windows版で見たのと違ってコメントが少ない……、というか変わってないのでは☆?」
「 設定ファイル全部 Windows版で上書きしてしまいましょう」
「 FileZilla では ユーザー・ホームディレクトリ以下しか見れない……☆」
# mv /home/muzudho/moving/nginx.conf /etc/nginx/nginx.conf
mv: `/etc/nginx/nginx.conf' を上書きしますか? y
# cat /etc/nginx/nginx.conf
nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# backend_node に LoopBack サーバー(localhost:3000)を追加
upstream backend_node {
ip_hash;
server 127.0.0.1:3000;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
# 存在するファイルパスが指定された場合はそのまま Nginx で処理
if ( -f $request_filename ) {
break;
}
# 存在しないファイルパスが指定された場合は backend_node で処理
if ( !-f $request_filename ) {
proxy_pass http://backend_node;
break;
}
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
「 curl で localhost:80
にアクセスしろだぜ☆」
# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# cd /etc/nginx
新しいメールが /var/spool/mail/root にあります
# dir
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf nginx.conf.rpmsave scgi_params uwsgi_params win-utf
「 あれっ、 conf.d
ってディレクトリだよな☆ 中に何かあるのか……☆」
# cd conf.d
# dir
default.conf default.conf.rpmsave
「 Windows版とフォルダー構成が違うのか☆(^~^)」
# vi default.conf
default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
「 サーバーを中に出したような感じをしている☆ 消すかだぜ☆(^~^)」
# rm default.conf
rm: 通常ファイル `default.conf' を削除しますか? y
# rm default.conf.rpmsave
rm: 通常ファイル `default.conf.rpmsave' を削除しますか? y
# curl localhost:80
「 設定ファイルを変更したら、サービスの再起動がいるのでは☆?」
# systemctl restart nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
新しいメールが /var/spool/mail/root にあります
# systemctl stop nginx
# systemctl start nginx
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
# systemctl status nginx.service
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since 日 2019-06-09 20:02:13 JST; 38s ago
Docs: http://nginx.org/en/docs/
Process: 106799 ExecStop=/bin/kill -s TERM $MAINPID (code=exited, status=0/SUCCESS)
Process: 106816 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Main PID: 106549 (code=exited, status=0/SUCCESS)
6月 09 20:02:13 localhost.localdomain systemd[1]: Starting nginx - high performance web server...
6月 09 20:02:13 localhost.localdomain nginx[106816]: nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (13: Permission denied)
6月 09 20:02:13 localhost.localdomain systemd[1]: nginx.service: control process exited, code=exited status=1
6月 09 20:02:13 localhost.localdomain systemd[1]: Failed to start nginx - high performance web server.
6月 09 20:02:13 localhost.localdomain systemd[1]: Unit nginx.service entered failed state.
6月 09 20:02:13 localhost.localdomain systemd[1]: nginx.service failed.
「 nginx.conf の 13行目で パーミッション拒否 があるぜ☆」
keepalive_timeout 65;
# nginx -s reload
nginx: [error] open() "/var/run/nginx.pid" failed (2: No such file or directory)
# ll
# chown root:root nginx.conf
# ll
# yum remove nginx
# find / -name nginx*
/etc/nginx/nginx.conf.rpmsave
# rm -rf /etc/nginx
# find / -name nginx*
/etc/yum.repos.d/nginx.repo
/var/lib/yum/repos/x86_64/7/nginx
/var/lib/yum/repos/x86_64/7/nginx-mainline
/var/lib/yum/repos/x86_64/7/nginx-stable
/var/log/nginx
/var/cache/yum/x86_64/7/nginx
/var/cache/yum/x86_64/7/nginx-mainline
/var/cache/yum/x86_64/7/nginx-stable
/var/cache/nginx
/usr/share/augeas/lenses/dist/nginx.aug
/usr/share/nginx
# rm /etc/yum.repos.d/nginx.repo
# rm -rf /var/lib/yum/repos/x86_64/7/nginx
# rm -rf /var/lib/yum/repos/x86_64/7/nginx-mainline
# rm -rf /var/lib/yum/repos/x86_64/7/nginx-stable
# rm -rf /var/log/nginx
# rm -rf /var/cache/yum/x86_64/7/nginx
# rm -rf /var/cache/yum/x86_64/7/nginx-mainline
# rm -rf /var/cache/yum/x86_64/7/nginx-stable
# rm -rf /var/cache/nginx
# rm -rf /usr/share/augeas/lenses/dist/nginx.aug
# rm -rf /usr/share/nginx
# find / -name nginx*
「 yellowdog updater modified のリポジトリを作成しましょう」
# vi /etc/yum.repos.d/nginx.repo
nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
「 去年のわたしが書いた設定を また使おう☆ インターネット上の薄味な情報とか頼りない☆」
# sudo yum install nginx
# nginx -V
nginx version: nginx/1.17.0
# cd /etc/nginx
# dir
conf.d fastcgi_params koi-utf koi-win mime.types modules nginx.conf scgi_params uwsgi_params win-utf
# cat nginx.conf
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# curl localhost:80
curl: (7) Failed connect to localhost:80; 接続を拒否されました
# systemctl start nginx
# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
# curl localhost:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>
「 localhost:80
で Nginx が動いていて、 localhost:3000
で Nodejs/Express が動いているぜ☆」
# vi /etc/nginx/nginx.conf
i
-- INSERT --
nginx.conf
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
# backend_node に LoopBack サーバー(localhost:3000)を追加
upstream backend_node {
ip_hash;
server 127.0.0.1:3000;
}
#gzip on;
include /etc/nginx/conf.d/*.conf;
}
# cd /etc/nginx/conf.d
# dir
default.conf
# vi default.conf
vi default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
# 存在するファイルパスが指定された場合はそのまま Nginx で処理
if ( -f $request_filename ) {
break;
}
# 存在しないファイルパスが指定された場合は backend_node で処理
if ( !-f $request_filename ) {
proxy_pass http://backend_node;
break;
}
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# systemctl reload nginx
# curl localhost:80
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>An error occurred.</h1>
<p>Sorry, the page you are looking for is currently unavailable.<br/>
Please try again later.</p>
<p>If you are the system administrator of this resource then you should check
the error log for details.</p>
<p><em>Faithfully yours, nginx.</em></p>
</body>
</html>
# curl http://127.0.0.1:80
# curl http://127.0.0.1:3000
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# cat /var/log/nginx/error.log
2019/06/09 20:58:13 [crit] 108746#108746: *2 connect() to 127.0.0.1:3000 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "localhost"
2019/06/09 21:00:38 [crit] 108746#108746: *4 connect() to 127.0.0.1:3000 failed (13: Permission denied) while connecting to upstream, client: 127.0.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3000/", host: "127.0.0.1"
nginx : Permission denied を解決
CentOS6.7 64bit SELinuxによるアクセス制御
[CENTOS7][NGINX] SELINUXを設定しホームディレクトリ配下のWEBコンテンツを公開を許可
# cat /etc/selinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=enforcing
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
「 日本の記事には 手順が載っていて 理解の助けにならない☆
アメリカマンの記事を読もう☆ nginx selinux failed
みたいなキーワードでググるぜ☆」
Using NGINX and NGINX Plus with SELinux
「 SELinux というのは Security-Enhanced Linux、セキュリティが強化されたリナックスのことで、
グーグル翻訳を使うと負けた気がするので 原文読む☆」
「 Linuxのパーミッションと言えば ディレクトリーやファイルに付いているが、
SELinux は プロセスにも アクセス権限を付けるもので、仕組みがでかそうだぜ☆」
「 今の問題を解消する手順を暗記して さっさと進めましょう」
【ざっくりと理解する】SELinuxとは?
SELinux を使おう.使ってくれ.
CentOS7/RHEL7でむやみにSELinuxポリシー追加せずにnginx apacheをEnforcingのまま動かす
「 日本の記事は 英文を劣化コピー翻訳した ザル が多いので なるべく英語を読む☆ what is selinux
」
What is SELinux and how does it work?
SELinux Documentation
SELinux Project Wiki
「 全部読んでると来月になってしまうので 触りながら進めてみよう☆
audit
というのは 見張ってセキュリティ判定するものらしく、ログに残してくれているらしい☆」
# fgrep nginx /var/log/audit/audit.log
type=AVC msg=audit(1560081638.217:461459): avc: denied { name_connect } for pid=108746 comm="nginx" dest=3000 scontext=system_u:system_r:httpd_t:s0 tcontext=system_u:object_r:ntop_port_t:s0 tclass=tcp_socket
type=SYSCALL msg=audit(1560081638.217:461459): arch=c000003e syscall=42 success=no exit=-13 a0=4 a1=55909ac1d2e8 a2=10 a3=7ffdf2a89140 items=0 ppid=108232 pid=108746 auid=4294967295 uid=996 gid=994 euid=996 suid=996 fsuid=996 egid=994 sgid=994 fsgid=994 tty=(none) ses=4294967295 comm="nginx" exe="/usr/sbin/nginx" subj=system_u:system_r:httpd_t:s0 key=(null)
system_u:system_r:httpd_t:s0
「 みたいなやつは、システム・ユーザー(_u)のシステム・ロール(_r)のHTTPDタイプ(_t)の s0 レンジに照らし合わせて
拒否されたようで、rootユーザーでWebページを見ようとしている操作に パターン・マッチしたようだぜ☆」
「 1つ1つ見ていく☆
nginx がどのようなパターンで監視されているか調べる☆
SELinux の用語としては、パターンではなく、ラベルと呼ぶようだぜ☆」
ps -eZ | grep nginx
system_u:system_r:httpd_t:s0 108232 ? 00:00:00 nginx
system_u:system_r:httpd_t:s0 108746 ? 00:00:00 nginx
新しいメールが /var/spool/mail/root にあります
「 nginx でのWebページへのアクセスは、 system_u:system_r:httpd_t:s0
ラベルが止めているようだぜ☆」
「 次に NodeJS の index.html ページのラベルを調べたいが、どこに index.html あるんだろうな☆?」
# cd /
# find index.html
find: ‘index.html’: そのようなファイルやディレクトリはありません
# find index.htm
find: ‘index.htm’: そのようなファイルやディレクトリはありません
# cd /var/www
-bash: cd: /var/www: そのようなファイルやディレクトリはありません
「 NodeJS/Express が動的にページ生成してるとしたら、index.htmlファイルは無いわよ?」
「 NodeJS ってどこにインストールされてるんだぜ☆?」
「 usr、share、local、webapps あっ、あった☆」
「 動的に生成されるページを、どうやってアクセス・コントロールするんだぜ☆?」
SELinuxで安心安全なnode.jsを目指していく
SELinux — Making it a Little Easier for Web
# getsebool -a | grep http
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
named_tcp_bind_http_port --> off
prosody_bind_http_port --> off
新しいメールが /var/spool/mail/root にあります
「 セキュリティを下げて、とりあえず ネットワークだけ許可するぜ☆」
# setsebool -P httpd_can_network_connect=1
新しいメールが /var/spool/mail/root にあります
# systemctl stop nginx
# systemctl start nginx
# curl localhost
<!DOCTYPE html><html><head><title>Express</title><link rel="stylesheet" href="/stylesheets/style.css"></head><body><h1>Express</h1><p>Welcome to Express</p></body></html>
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント