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

相关文章
相关标签/搜索