CentOS7.3におけるYumによるNginxのインストールと設定

Nginxの導入

# 情報を確認する
yum info nginx

yum info httpd

# Apacheであるhttpdを削除する
yum remove httpd -y

# Nginxをインストールする
yum install nginx -y

# Nginxを起動時に自動実行するように設定
chkconfig nginx on

# 自動起動の状態を確認
chkconfig

# Nginxサービスを起動
service nginx start

# ポートのリッスン状況を確認
netstat -ntl

# この段階でブラウザからアクセスしてみる
# 例: http://IPアドレス

# アクセスができない場合はpingを試す
# またはfirewalldファイアウォールの状態を確認
起動: systemctl start firewalld
状態確認: systemctl status firewalld
停止: systemctl disable firewalld
無効化: systemctl stop firewalld

CentOS7ではfirewalldが使用されます。

firewalldはデフォルトでインストールおよび有効化されており、Nginxがアクセスできるようにするには以下のコマンドを実行する必要があります:

firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --zone=trusted --add-port=80/tcp
<br></br><br></br><br></br><br></br>補足: Aliyunをご利用の場合<br></br>セキュリティグループで80番ポートへのアクセスを許可するルールを追加してください。設定しないと正常に動作しません。<br></br><br></br>

Nginxのリバースプロキシ設定
==================

> **/etc/nginx/nginx.conf**

<div>```
user              nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;

events {
    use epoll;  
    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  0;
    keepalive_timeout  65;

    gzip  on;

    # /etc/nginx/conf.d ディレクトリから設定ファイルを読み込む
    # デフォルトサーバーは conf.d/default.conf にあります
    include /etc/nginx/conf.d/*.conf;

    include     upstream.conf;
    include     cncounter.com.conf; 

}

負荷分散のための設定: /etc/nginx/upstream.conf

upstream www.cncounter.com {
    server 127.0.0.1:8080;
}

サイト設定ファイル: /etc/nginx/cncounter.com.conf

server
{
    listen         80;
    server_name    www.cncounter.com;
    index index.jsp;
    root    /usr/local/cncounter_webapp/cncounter/;
    location ~ ^/NginxStatus/ {
    stub_status on;
    access_log off;
    }

    location / {
         root    /usr/local/cncounter_webapp/cncounter/;
         proxy_redirect off ;
         proxy_set_header Host $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header REMOTE-HOST $remote_addr;
         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
         client_max_body_size 50m;
         client_body_buffer_size 256k;
         proxy_connect_timeout 30;
         proxy_send_timeout 30;
         proxy_read_timeout 60;
         proxy_buffer_size 256k;
         proxy_buffers 4 256k;
         proxy_busy_buffers_size 256k;
         proxy_temp_file_write_size 256k;
         proxy_next_upstream error timeout invalid_header http_500 http_503 http_404;
         proxy_max_temp_file_size 128m;
         proxy_pass    http://www.cncounter.com;
    }
}

サービスの再起動

service nginx stop

service nginx start
 

タグ: CentOS Yum nginx firewalld リバースプロキシ

7月16日 00:45 投稿