質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

Q&A

解決済

1回答

6643閲覧

nginx 設定 http://localhost:8000 を http://localhost/reminiscence でもアクセスできるようにしたい

roswell

総合スコア17

nginx

nginixは軽量で高性能なwebサーバーの1つです。BSD-likeライセンスのもとリリースされており、あわせてHTTPサーバ、リバースプロキシ、メールプロキシの機能も備えています。MacOSX、Windows、Linux、上で動作します。

0グッド

1クリップ

投稿2019/01/08 01:37

nginxの設定で、

http://localhost:8000
http://XX.2.65.170:8000/

で、インストールしたweb app "reminiscense"
の画面になるのですが、

http://localhost/reminiscence
http://xx.2.65.170/reminiscence

でも、同様な動きをするには、nginxの設定をどのようにすればよいですか?

server { ... location / { # proxy_pass http://127.0.0.1:8000; proxy_pass http://127.0.0.1/reminiscence;

などにしてみましたが、希望の動作にはならないので、
御教示いただけますでしょうか?
設定ファイルは、下記になります。

~ $ cat /etc/nginx/nginx.conf user www-data; worker_processes auto; pid /run/nginx.pid; include /etc/nginx/modules-enabled/*.conf; events { worker_connections 768; # multi_accept on; } http { ## # Basic Settings ## sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # server_tokens off; # server_names_hash_bucket_size 64; # server_name_in_redirect off; include /etc/nginx/mime.types; default_type application/octet-stream; ## # SSL Settings ## ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE ssl_prefer_server_ciphers on; ## # Logging Settings ## access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log; ## # Gzip Settings ## gzip on; gzip_disable "msie6"; # gzip_vary on; # gzip_proxied any; # gzip_comp_level 6; # gzip_buffers 16 8k; # gzip_http_version 1.1; # gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; ## # Virtual Host Configs ## include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; } #mail { # # See sample authentication script at: # # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript # # # auth_http localhost/auth.php; # # pop3_capabilities "TOP" "USER"; # # imap_capabilities "IMAP4rev1" "UIDPLUS"; # # server { # listen localhost:110; # protocol pop3; # proxy on; # } # # server { # listen localhost:143; # protocol imap; # proxy on; # } #} ~ $ cat /etc/nginx/conf.d/reminiscence.conf #worker_processes 2; #events { # worker_connections 1024; #} #http { # include mime.types; # default_type application/octet-stream; # sendfile on; # sendfile_max_chunk 512k; # keepalive_timeout 65; # proxy_read_timeout 300s; server { listen 80; server_name localhost; client_max_body_size 1024m; location /static/ { root /home/yama/reminiscence/venv/reminiscence; # root of project directory aio threads; } location = /favicon.ico { access_log off; log_not_found off; } location / { proxy_pass http://127.0.0.1:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; root /home/yama/reminiscence/venv/reminiscence; #root of project directory } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } #}

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

/etc/nginx/conf.d/, /etc/nginx/sites-enabled/ 以下のファイル (default.conf など) が有効になっているのでしょう。
バーチャルホストになっているので、server_name を適切に設定し、そのホスト名でアクセスするか、default.conf などが不要であれば、削除していいと思います。

(2019/01/09 18:11) 追記

http://XX.2.65.170/reminiscence/http://127.0.0.1:8000/ にプロキシーする設定です。

location /reminiscence/ { proxy_pass http://127.0.0.1:8000/; # 最後のスラッシュ(/)が必要。 (略) }

どうしても、http://XX.2.65.170/〜 ではなく http://localhost/〜 でアクセスしたいのであれば、既存の(default.conf などの)設定を変更するか削除するしかありません。

投稿2019/01/09 02:55

編集2019/01/09 09:11
TaichiYanagiya

総合スコア12141

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

roswell

2019/01/09 07:42

> バーチャルホストになっているので、server_name を適切に設定し、 yama@jpx20120007:~$ grep server_name /etc/nginx/conf.d/reminiscence.conf server_name reminiscence; に、変更しましたが、 http://localhost/reminiscence で、"Not Found" になってしまいます。 virtual hostとserver_name で、web検索していますが、 いずれも localhost 部分に相当する設定変更で、 今回の場合の、設定がうまくみつからず。 具体的に、どのように変更すれば、いいかご教示いただけますでしょうか?
TaichiYanagiya

2019/01/09 08:08

server_name は URL のホスト部に該当しますので、"server_name reminiscence;" と設定したならば、http://reminiscence/ でアクセスする必要があります(reminiscence の名前解決で IPアドレスが引ける必要もあります)。 手っ取り早く、"server_name XX.2.65.170;" (127.0.0.1 以外でサーバーに付いているIPアドレス)を設定して、http://XX.2.65.170/ でアクセスするといいのではないでしょうか。
roswell

2019/01/09 08:41

>server_name は URL のホスト部に該当しますので、"server_name reminiscence;" と設定したならば、http://reminiscence/ でアクセスする必要があります(reminiscence の名前解決で IPアドレスが引ける必要もあります)。 ご教示ありがとうございます。 DNSで引けないと無理なのは知っていましたが... もしやと思い.. >手っ取り早く、"server_name XX.2.65.170;" (127.0.0.1 以外でサーバーに付いているIPアドレス)を設定して、 それもわかるのですが、 要は、サーバーアプリが増えてきたら、これも対応できないので、 http://localhost:8000 は問題ないので、http://localhost/reminiscence でアクセスできるような設定が有るのかという、最初の質問に戻ってしまいます。
roswell

2019/01/10 01:34

> http://XX.2.65.170/reminiscence/http://127.0.0.1:8000/ にプロキシーする設定です。 試してみたのですが、症状変わらずになってしまいます。 yama@jpx20120007:/etc/nginx/conf.d$ grep -e location -e proxy_pass reminiscence.conf # location /reminiscence/ { location = /favicon.ico { access_log off; log_not_found off; } location /reminiscence/ { proxy_pass http://127.0.0.1:8000/; location = /50x.html { yama@jpx20120007:/etc/nginx/conf.d$ sudo nginx -t nginx: [warn] conflicting server name "_" on 0.0.0.0:80, ignored nginx: [warn] conflicting server name "_" on [::]:80, ignored nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful yama@jpx20120007:/etc/nginx/conf.d$ sudo service nginx restart yama@jpx20120007:/etc/nginx/conf.d$ tail -n 0 -f /var/log/nginx/access.log xx.2.65.170 - - [10/Jan/2019:10:08:42 +0900] "GET /reminiscence/ HTTP/1.1" 404 200 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36"rt=0.000 uct="-" uht="-" urt="-"
TaichiYanagiya

2019/01/10 14:30

まだ、既存の(default.conf などの)設定が使われているようです。 server_name を確認ください。 access_log を分ければ、どちらの server { } の設定が使われているか確認できます。
roswell

2019/01/11 05:43

> /etc/nginx/conf.d/, /etc/nginx/sites-enabled/ 以下のファイル (default.conf など) が有効になっているのでしょう。 の部分の御指摘を見おとしていました。 /etc/nginx/sites-enabled/default(linkファイル)を削除、nginx 再起動し、 http://XX.2.65.170/reminiscence で、やっと、アクセスできました。 御教示、ありがとうございます。 ですが、 http://XX.2.65.170:8000 の時に表示されていた、背景やボタン等が表示されなくなってしまいました。 ログより、 yama@jpx20120007:~$ tail -n 0 -f /var/log/nginx/error.log 2019/01/11 13:52:00 [error] 11232#11232: *1 open() "/usr/share/nginx/html/static/css/bootstrap.min.css" failed (2: No such file or directory), client: XX.2.65.170, server: localhost, request: "GET /static/css/bootstrap.min.css HTTP/1.1", host: "XX.2.65.170", referrer: "http://XX.2.65.170/reminiscence/" 2019/01/11 13:52:00 [error] 11232#11232: *2 open() "/usr/share/nginx/html/static/folder.svg" failed (2: No such file or directory), client: XX.2.65.170, server: localhost, request: "GET /static/folder.svg HTTP/1.1", host: "XX.2.65.170" ... location /reminiscence/ { proxy_pass http://127.0.0.1:8000/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; root /home/yama/reminiscence/venv/reminiscence; #root of project directory } の root /home/yama/reminiscence/venv/reminiscence; #root of project directory からではなく、 /usr/share/nginx/html/ 以下からでファイルを参照している事によるものかと思います。 /etc/nginx/sites-enabled/default を削除したとしても、 root /home/yama/reminiscence/venv/reminiscence が、reminiscenceの以降処理のrootとして用いられるにはどうすればよいか、 御教示いただけます?
TaichiYanagiya

2019/01/11 10:00

Location ヘッダであれば proxy_redirect (http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_redirect) で変換できます。 設定なしでも、proxy_pass の逆変換をやってくれます。 しかし、HTML のリンクについては変換できません。 リンクが絶対パスで(/ で始まっていて)、かつ、/reminiscence/ で始まっていないものについては "location /reminiscence/ { }" 箇所では扱えません。 アプリケーション側でリンクのパスを相対パスにする、もしくは、 /reminiscence/ で始まるよう設定できませんでしょうか? 最終手段として、sub_filer (http://nginx.org/en/docs/http/ngx_http_sub_module.html#sub_filter) で「/」→「/reminiscence/」に変換できるかもしれません。
roswell

2019/01/11 10:23

> HTML のリンクについては変換できません。 そうなんですね。 >アプリケーション側でリンクのパスを相対パスにする、もしくは、 >/reminiscence/ で始まるよう設定できませんでしょうか? https://github.com/kanishka-linux/reminiscence のものなので、ちょっと自力では対応できないです。 ドキュメント等みてみます。 >最終手段として、sub_filer ...で「/」→「/reminiscence/」に変換できるかもしれません。 調べてみます。
roswell

2019/01/11 11:28

動きましたーーー。 ご教示いただいた設定を反映する前に、 いろいろ reminiscence.conf を弄っていて > location /static/ { > root /home/yama/reminiscence/venv/reminiscence; # root of project の部分を location /reminiscence/ にして、nginx -t でエラーが出たので、 location /reminiscence/ ここを # でコメント行にしていました。 error_log をじっくり眺めて、/usr/share/nginx/html/static/  static というキーワードを思い出し、 location /static/ に戻し、 コメントアウトを外して、nginx を再起動 して確認したところ、ただしく動作しました。 ご教示、本当にありがとうございました。 と同時に、default を削除した結果、 別の appli (Sitebar)が動かなくなり、共存できなくなったので、 今、調査中です。
TaichiYanagiya

2019/01/12 08:18

その解決方法だと、reminiscence はいいですが、他のアプリケーションを追加するときに困るのではないでしょうか。 パスベースで分けるのではなく、ホストベース(バーチャルホスト)で分けた方が問題は少ないと思います。
roswell

2019/01/16 06:22

> ホストベース(バーチャルホスト)で分けた方が問題は少ないと思います。 https://webkaru.net/linux/nginx-default-server/ を参考にすると、 server_name example1.com; server_name example2.com; server_name example3.com; のように、server毎に、server_name を変えるということですよね。 DNSできないので、ipベースでのURLアクセス時では、server_name を どのように変えればよいのでしょうか? 後、下記の conf ファイルで、 reminiscenceは動いているのですが、 sitebartは、動作しないので、 http://xx.2.65.170/sitebar で、sitebarが動作するようにするには、 どのように変えればよいかご教示頂けますか? いろいろ試行しているのですが、なかなか動作せずで。 yama@jpx20120007 /e/n/conf.d> ls *.conf -rw-r--r-- 1 yama root 617 1月 16 14:43 reminiscence.conf -rw-r--r-- 1 yama root 513 1月 16 14:57 sitebar.conf yama@jpx20120007 /e/n/conf.d> cat reminiscence.conf server { listen 80; server_name localhost; client_max_body_size 1024m; location /static/ { root /home/yama/reminiscence/venv/reminiscence; # root of project directory aio threads; } location = /favicon.ico { access_log off; log_not_found off; } location /reminiscence/ { proxy_pass http://127.0.0.1:8000/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; root /home/yama/reminiscence/venv/reminiscence; #root of project directory } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } yama@jpx20120007 /e/n/conf.d> cat sitebar.conf # Virtual Host configuration for example.com # # You can move that to a different file under sites-available/ and symlink that # to sites-enabled/ to enable it. # server { listen 80; server_name localhost; # pass PHP scripts to FastCGI server # 追加 Default server configuration のphp部分 php-fpmに変更済 # location /sitebar/ { root /var/www/html; proxy_pass http://127.0.0.1/sitebar/; index index.php; # index を、index.phpに fastcgi_pass unix:/run/php/php7.0-fpm.sock; } } yama@jpx20120007 /e/n/conf.d> ls /var/www/html/ 合計 24 drwxr-xr-x 4 root root 4096 1月 10 10:07 ./ drwxr-xr-x 3 root root 4096 12月 18 16:27 ../ -rw-r--r-- 1 root root 612 12月 18 16:27 index.nginx-debian.html -rw-r--r-- 1 yama yama 19 12月 25 17:16 php_info.php drwxr-xr-x 2 root root 4096 12月 27 13:30 php_test/ drwxr-xr-x 12 yama yama 4096 12月 27 11:27 sitebar/ yama@jpx20120007 /e/n/conf.d> ls /var/www/html/sitebar/index.php -rw-r--r-- 1 yama yama 4350 12月 24 15:45 /var/www/html/sitebar/index.php
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問