CentOS7下Nginx安裝、配置、調優

1、Nginx安裝

1.到nginx官網下載最新包http://nginx.org/

2.解壓nginx-1.10.0.tar.gz到/usr/local/ngxin

3.進入/ngxin文件夾,執行初始化操做,命令以下:   

./configure --prefix=/usr/local/nginx
make
make install

    安裝完成後文件目錄以下:php

4.啓動nginx

cd /usr/local/nginx/sbin

    輸入./nginx啓動nginx。若是nginx被配置成了服務能夠這樣啓動/etc/init.d/nginx starthtml

5.可能出現的問題

    1.缺乏依賴,nginx須要依賴:

        gzip 模塊須要 zlib 庫 ( 下載: http://www.zlib.net/ )  zlib-1.2.8.tar.gz,
        rewrite 模塊須要 pcre 庫( 下載: http://www.pcre.org/ )  pcre-8.21.tar.gz,
        ssl 功能須要 openssl 庫( 下載: http://www.openssl.org/ )  openssl-1.0.1.tar.gz。linux

    Nginx 安裝配置nginx

    2.報錯:./nginx: error while loading shared libraries: libpcre.so.1: cannot open shared object file: No such file or directory       

若是是32位系統
[root@iZ11y6td7sjZ lib]# ln -s /usr/local/lib/libpcre.so.1 /lib
若是是64位系統
[root@iZ11y6td7sjZ lib]# ln -s /usr/local/lib/libpcre.so.1 /lib64
而後在啓動nginx就OK了

2、Nginx配置

    1.Nginx反向代理Tomcat

        tomcat配置文件server.xml文件以下web

    

    配置hosts文件,內容以下:apache

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

#域名對應的網址
47.105.137.13   ctyl.cloud-info.cn
#zookeeper servers
47.105.137.13 edu-provider-01

    在/usr/local/nginx下的conf文件夾裏,配置nginx.conf文件tomcat

具體內容以下:服務器

user  root;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


upstream tomcat {
        server 47.105.137.13:8080;
}
upstream tomcat {
        server 47.105.137.13:8081;
}
    server {
        listen       80;
        server_name  ctyl.cloud-info.cn;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_set_header        Host $host;
            root   /usr/local/tomcat/apache-tomcat-8.5.34/webapps/ctyl01;
            index  index.html index.htm;
        }
error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   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;
        #}
    }
 server {
        listen       8081;
        server_name  www.wubingyin.cn;

         location / {
            proxy_set_header        Host $host;
            root   /usr/local/tomcat/apache-tomcat-8.5.34/webapps/blog-vote;
            index  index.html index.htm;
        }

         error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
}

}

這裏設置user屬性爲root,是由於後面訪問網址的時候,nginx報錯403 forbidden (13: Permission denied)

1、因爲啓動用戶和nginx工做用戶不一致所致app

    解決方案:將nginx.config的user改成和啓動用戶一致,dom

2、缺乏index.html或者index.php文件,就是配置文件中root指定的路徑下缺乏index index.html index.htm這行中的指定的文件。

3、權限問題,若是nginx沒有web目錄的操做權限,也會出現403錯誤。

    解決方案:修改web目錄的讀寫權限,或者是把nginx的啓動用戶改爲目錄的所屬用戶,重啓Nginx便可解決

4、SELinux設置爲開啓狀態(enabled)的緣由。

    查看當前selinux的狀態。

         /usr/sbin/sestatus

    將SELINUX=enforcing 修改成 SELINUX=disabled 狀態。

        vi /etc/selinux/config

        SELINUX=disabled

    重啓生效。reboot。

        reboot

        server:服務器IP:端口

        server name:對應tomcat的HOST name

        root:項目路徑

        proxy_set_header        Host $host; 這一句必須有,不然代理不了.設置請求頭,並將請求頭信息傳到服務器,訪問服務器文件時不用帶上端口號了

3、Nginx調優

    worker_processes  1;//CPU數量*核數

    server {
    listen 8080;                //監聽的端口
    server_name  ctyl.cloud-info.cn;    //監聽域名。
    location / {    //域名指定項目指向哪裏

        root     /var/local/aqweq       

        index  index.html index.htm;

        proxy_pass http://47.105.137.13:8080;         proxy_redirect off;         proxy_set_header Host $host;         proxy_set_header X-Real-IP $remote_addr;         proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;         client_max_body_size 10m;         client_body_buffer_size 128k;         proxy_connect_timeout 90;         proxy_read_timeout 90;         proxy_buffer_size 4k;         proxy_buffers 6 32k;         proxy_busy_buffers_size 64k;         proxy_temp_file_write_size 64k;     } }

相關文章
相關標籤/搜索