Help us understand the problem. What is going on with this article?

VirtualBoxでUbunts上のnginxでリバースプロキシしてHTMLを書き換えてHello WorldをCrazy Worldにする

はじめに

VirtualBoxでCentOS上のApacheでリバースプロキシしてHTMLを書き換えてHello WorldをCrazy WorldにするのNginxバージョンです。

やったこと

nginxで立ち上げたhttp://localhostにnodeで立ち上げたhttp://localhost:3000/のHTMLを書き換えて表示させる
Screenshot from 2019-08-23 11-30-08.png

nginxの設定

sub_filter '置き換え元文字' "置き換え後文字" ;

nginx.conf
location / {
            # root   html;
            # index  index.html index.htm;
            sub_filter_once off;
            sub_filter 'Hello' 'Crazy' ;
            sub_filter '</head>' "<script>alert('Crazy World')</script></head>" ;
            sub_filter 'nginx' 'Crazy' ;
            proxy_pass   http://127.0.0.1:3000;
        }
/etc/nginx/sites-available/default
location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            # try_files $uri $uri/ =404;
            sub_filter_once off;
            sub_filter 'Hello' 'Crazy' ;
            sub_filter '</head>' "<script>alert('Crazy World')</script></head>" ;
            sub_filter 'nginx' 'Crazy' ;
            proxy_pass   http://127.0.0.1:3000;
        }

nodeで立ち上げるHTMLを準備

hello.html
<!DOCTYPE html>
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
        <h1>Hello World</h1>
    </body>
</html>
hello.js
const http = require('http');
const fs = require('fs');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  // res.setHeader('Content-Type', 'text/plain');
  // res.end('Hello World!\n');
  fs.readFile('hello.html','UTF-8',
  (error, data)=>{
    res.writeHead(200,{'Content-Type':'text/html'});
    res.write(data);
    res.end();
  });
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

nodeで立ち上げてアクセスhttp://localhost:3000/

aoui@aoui-VirtualBox:/media/sf_reverse_proxy_nginx/nginx/public$ node hello.js
Server running at http://127.0.0.1:3000/

Screenshot from 2019-08-23 11-13-54.png

http://localhostにアクセスしたらHello WorldがCrazy Worldになって表示できた

Screenshot from 2019-08-23 11-14-10.png

ポートが使えない時

他のプロセスがポートを占有してnginxを再起動できない

Dockerでやりたい

docker-composeで立ち上げて、nginxでリバースプロキシしてHTMLを書き換えてHello WorldをCrazy Worldにする

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away