5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【自分用】ubuntu nginxの設定

Last updated at Posted at 2016-05-14

webサーバーはapacheかnginx。アプリケーションサーバーは各言語ごとにある。

nginx.conf  ->  nginx起動時に読み込まれる、全体の設定ファイル
sites-available -> nginx.confに追加で設定したい内容を書く
sites-enabled -> sites-availableの内容をシンボリックリンクで貼り付けして設定
nginx
├── nginx.conf
├── uwsgi_params
├── scgi_params
├── fastcgi_params
├── mime.types
├── conf.d
│   └── default.conf
├── nginx.conf
├── sites-available
│   └── default
└── sites-enabled
    └── default -> /etc/nginx/sites-available/default

ポートを元から使ってる場合は切り替えの必要あり。

例:80番ポートはデフォルトのApacheなどで使ってるので他のポートを使うなど。
listen 8000;
http://askubuntu.com/questions/599888/service-nginx-start-is-ok-but-nginx-not-running

エラー例
sudo service nginx start
Starting nginx: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] still could not bind()

netstatコマンド

開いているポートを確認

lsofコマンド

8000

プロセスで使われてるポートを調べる
http://www.atmarkit.co.jp/ait/articles/1510/05/news014.html

#静的ページの設定例

/etc/nginx/sites-available/default
server {
    listen 8000 default_server;
    listen [::]:8000 default_server ipv6only=on;
    server_name  [任意のドメイン名];
    location / {
        root   /usr/share/nginx/html/[任意のパス]/;
        index  index.html index.htm;
    } 
}
/etc/nginx/nginx.conf
# この方法ではnginxのユーザーと、プロセスの数を指定します
user [任意のユーザー];
worker_processes  1;

# NginxのエラーログとプロセスIDを保存する場所を指定します
error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
  worker_connections  1024;
  # もし複数のプロセスで実行する場合はonにします
  accept_mutex off;
}

http {
  include       /etc/nginx/mime.types;

  default_type application/octet-stream;
  access_log /tmp/nginx.access.log combined;
 
  # カーネルのsendfileを使います
  sendfile        on;
  # HTTPヘッダーの前にsendfile()を用意します
  tcp_nopush     on;

  keepalive_timeout  5;
  tcp_nodelay        on;

  gzip  on;
  gzip_vary on;
  gzip_min_length 500;
  
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";
  gzip_types text/plain text/xml text/css
     text/comma-separated-values
     text/javascript application/x-javascript
     application/atom+xml image/x-icon;

  # バーチャルホストを指定します
  server {
    # あなたのドメインに置き換えてください
    server_name gyungyun.red;
    # Sinatraのpublicディレクトリの絶対パスを指定します
    root /usr/share/nginx/html/[任意のパス]/public/;
    # リクエストが通るポート番号を指定します
    listen 8000;
    # クライアントからのリクエストの最大値を設定します
    client_max_body_size 4G;
    # サーバーはこの時間を過ぎたら接続を閉じます
    keepalive_timeout 5;

    location / {
      try_files $uri @app;
       root   /usr/share/nginx/html/[任意のパス]/public/;
        index  index.html index.htm;
    }

    location @app {
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      # pass to the upstream unicorn server mentioned above 
      proxy_pass http://unicorn_server;
    }
  }
}

#参考サイト
nginx公式サイト(日本語)
http://mogile.web.fc2.com/nginx_wiki/community/index.html
sinatraサイト(日本語)
http://recipes.sinatrasapporo.org/

5
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?