Die in diesem Artikel verwendete Linux-Distribution: CentOS 6.7. Download-URL:https://wiki.centos.org/Download

1. Nginx installieren

Quelle herunterladen: wget http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

Quelle installieren: yum install nginx-release-centos-6-0.el6.ngx.noarch.rpm -y (beachten Sie den Parameter -y)

Nginx installieren: yum install nginx

Nginx-Dienst starten: service nginx start

Nginx-Dienst stoppen: service nginx stop

Nginx-Laufstatus prüfen: service nginx status

Nginx-Konfigurationsdatei prüfen: nginx -t

Konfiguration bei laufendem Dienst neu laden: nginx -s reload

Nginx-Dienst zum Autostart hinzufügen: chkconfig nginx on

2. Firewall-Regeln ändern

Firewall-Konfiguration des Hosts ändern, auf dem Nginx läuft: vi /etc/sysconfig/iptables, und den von Nginx verwendeten Port zur Liste der erlaubten Ports hinzufügen.

Zum Beispiel: -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT (bedeutet, Port 80 zuzulassen)

Firewall-Konfiguration des Hosts ändern, auf dem Tomcat läuft: vi /etc/sysconfig/iptables, und den von Tomcat verwendeten Port zur Liste der erlaubten Ports hinzufügen.

Zum Beispiel: -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT (bedeutet, Port 8080 zuzulassen)

Wenn sich mehrere Tomcats auf dem Host befinden, fügen Sie auf diese Weise mehrere Regeln hinzu und ändern Sie die entsprechenden Portnummern.

Nach dem Speichern die Firewall neu starten: service iptables restart

3. Konfiguration der Tomcat-Lastverteilung

Wenn Nginx startet, lädt es standardmäßig die Konfigurationsdatei /etc/nginx/nginx.conf, und nginx.conf referenziert alle .conf-Dateien im Verzeichnis /etc/nginx/conf.d.

Daher können Sie einige benutzerdefinierte Konfigurationen in eine separate .conf-Datei schreiben. Solange die Datei im Verzeichnis /etc/nginx/conf.d abgelegt wird, ist die Wartung komfortabel.

Erstellen Sie tomcats.conf: vi /etc/nginx/conf.d/tomcats.conf, der Inhalt ist wie folgt:

upstream tomcats {
     ip_hash;
    server 192.168.0.251:8080;
     server 192.168.0.251:8081;
     server 192.168.0.251:8082;
 }

Ändern Sie default.conf: vi /etc/nginx/conf.d/default.conf, ändern Sie wie folgt:

#注释原有的配置
#location / {
#    root   /usr/share/nginx/html;
#    index  index.html index.htm;
#}

#新增配置默认将请求转发到tomcats.conf配置的upstream进行处理
location / {
    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;
    proxy_pass http://tomcats; #与tomcats.conf里配置的upstream同名
}

Nach dem Speichern die Konfiguration neu laden:nginx -s reload

4. Konfiguration zur Trennung statischer Ressourcen

Ändern Sie default.conf: vi /etc/nginx/conf.d/default.conf, fügen Sie die folgende Konfiguration hinzu:

#所有js,css相关的静态资源文件的请求由Nginx处理
location ~.*\.(js|css)$ {
    root    /opt/static-resources; #指定文件路径
    expires     12h; #过期时间为12小时
}
#所有图片等多媒体相关静态资源文件的请求由Nginx处理
location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
    root    /opt/static-resources; #指定文件路径
    expires     7d; #过期时间为7天
}

5. SELinux-Sicherheitsregeln ändern

Wenn beim Zugriff auf Nginx ein 502 Bad Gateway-Fehler auftritt, kann dies durch SELinux auf dem Nginx-Host verursacht werden, das seine HTTP-Zugriffsberechtigungen einschränkt. Geben Sie den Befehl setsebool -P httpd_can_network_connect 1 ein, um die Berechtigung zu aktivieren.

Die vollständige Konfiguration der Datei /etc/nginx/nginx.conf lautet wie folgt:

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
worker_rlimit_nofile    100000;


events {
    use epoll;
    multi_accept on; 
    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;
    server_tokens off;
    #tcp_nopush     on;

    keepalive_timeout  65;

    gzip on;
    gzip_disable "msie6";
    gzip_static on;
    gzip_proxied any;
    gzip_min_length 1000;
    gzip_comp_level 4;
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    include /etc/nginx/conf.d/*.conf;
}

Die vollständige Konfiguration der Datei /etc/nginx/conf.d/default.conf lautet wie folgt:

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/log/host.access.log  main;

    #location / {
    #    root   /usr/share/nginx/html;
    #    index  index.html index.htm;
    #}

    location / {
        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;
        proxy_pass http://web_servers;
    }

    location ~.*\.(js|css)$ {
        root    /opt/static-resources;
        expires     12h;
    }

    location ~.*\.(html|jpg|jpeg|png|bmp|gif|ico|mp3|mid|wma|mp4|swf|flv|rar|zip|txt|doc|ppt|xls|pdf)$ {
        root    /opt/static-resources;
        expires     7d;
    }

    #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;
    #}
}

Hinweis:Wenn Sie beim Ausführen von Befehlen keine Root-Rechte haben, fügen Sie sudo vor den Befehlen hinzu.