Linux environment to install and configure Nginx reverse proxy server

First installed Nginx tossing most of the day beginning when installed the wrong version of the problem has been wrong version also cut can not
later be forced to reinstall the system to finally give I installed a

Linux distributions: CentOS 7

First install the build tools and libraries:

yum -y install make zlib zlib-devel gcc-c++ libtool  openssl openssl-devel

Then install pcre library allows Nginx Rewrite support functions:

cd /usr/local/src/ #移动到指定目录
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz #下载PCRE安装包
tar zxvf pcre-8.35.tar.gz #解压安装包
然后进入安装包目录
./configure #编译安装
make && make install #编译安装

Finally, install Nginx:

wget http://nginx.org/download/nginx-1.6.2.tar.gz #下载Nginx压缩包
tar xzf nginx-1.6.2.tar.gz #解压Nginx源码包
cd nginx-1.6.2 #然后进入解压出的目录
./configure --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35 #编译安装
make #编译安装
make install #编译安装

After installing View Nginx version

/usr/local/webserver/nginx/sbin/nginx -v #查看Nginx版本

Here Insert Picture Description

Successful installation


Then is configured on the server:

Create Nginx running user www

/usr/sbin/groupadd www
/usr/sbin/useradd -g www www

Will replace /conf/nginx.conf profile for the following:

For example, to configure the number of CPU cores and domain name according to their actual situation

user www www; # 定义Nginx运行的用户和组

worker_processes 1; # 启动进程数 设置值和CPU核心数一致

error_log /usr/local/webserver/nginx/logs/nginx_error.log info; # 全局错误日志的位置和日志级别
#错误日志等级:[debug | info | notice | warn | error | crit]

pid /usr/local/webserver/nginx/nginx.pid; # pid文件位置

worker_rlimit_nofile 65535; # 一个Nginx进程打开的最大文件描述符数目

#工作模式及连接数上限
events
{
  use epoll; # epoll是多路复用IO的一种方式 可大大提高Nginx性能
  worker_connections 65535; # 单个后台进程的最大并发连接数(最大连接数=连接数*进程数)
  multi_accept on; # 尽可能多的接受请求
}

# 设定http服务器 利用其反向代理功能提供负载均衡支持
http
{
  include mime.types; # 设定mine类型 类型由mine.type文件定义
  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';

  #charset gb2312;

  server_names_hash_bucket_size 128;
  client_header_buffer_size 32k; # 请求缓冲
  large_client_header_buffers 4 32k; # 请求缓冲
  client_max_body_size 8m; # 允许客户端请求的最大单文件字节数
  client_body_buffer_size 128k; # 缓冲区代理缓冲用户端请求的最大字节数

  sendfile on; # 指定Nginx是否调用sendfile函数输出文件 对于普通应用必须设为on
  tcp_nopush on; # 防止网络阻塞
  keepalive_timeout 60; # 超时时间 即客户端到服务器端的连接持续有效时间 节省资源
  tcp_nodelay on; # 提高数据的实时响应性
  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 64k;
  fastcgi_buffers 4 64k;
  fastcgi_busy_buffers_size 128k;
  fastcgi_temp_file_write_size 128k;

  # 开启gzip压缩 Nginx可以压缩静态资源
  gzip on;
  gzip_min_length 1k;
  gzip_buffers 4 16k;
  gzip_http_version 1.0;
  gzip_comp_level 2; # 压缩级别 最大为9 值越小则压缩后比例越小 CPU处理更快
  gzip_types text/plain application/x-javascript text/css application/xml; # gzip压缩的文件类型
  gzip_vary on;
  #limit_zone crawler $binary_remote_addr 10m;

  proxy_connect_timeout 90; # Nginx和后端服务器连接超时时间
  proxy_send_timeout 90; # 后端服务器数据回传时间
  proxy_read_timeout 90; # 连接成功后 后端服务器的响应时间
  # proxy_busy_buffers_size 64k; # 高负荷下缓冲区大小

  # include vhosts.conf 包含其它配置文件

    #server虚拟主机的配置
    server
     {
       listen 80; #监听端口

       server_name www.test.com; #域名

       index index.html index.htm index.php; # 首页文件名称
       root /usr/local/webserver/nginx/html; #服务器默认网站根目录位置

       # 默认访问请求
       location /
       {
         root /usr/local/webserver/nginx/html;
         index index.html index.htm index.php;
       }

       # 错误提示页
       error_page 500 502 503 504 /50x.html;
       location = /50x.html
       {
         root /usr/local/webserver/nginx/html;
       }


       # PHP脚本请求全部转发到FastCGI处理 使用FastCGI默认配置
       location ~ .*\.(php|php5)?$
       {
         root /root;
         fastcgi_pass 127.0.0.1:9000;
         fastcgi_index index.php;
         include fastcgi.conf;
       }

       # Nginx动静分离 静态页面直接从Nginx发布目录读取
       location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|ico)$
       {
         expires 30d; # 用户的浏览器的缓存时间 可节省带宽和环节服务器压力
         access_log off;
       }

       # 查看Nginx状态
       location /NginxStatus
       {
         stub_status on;
       }


       location ~ .*\.(js|css)?$
       {
         expires 15d;
         access_log off;
       }

    access_log off;
  }
}

Configuration profiles nginx.conf finished checking the correctness:

/usr/local/webserver/nginx/sbin/nginx -t

Here Insert Picture Description
Profiles normal

Some common commands:

/usr/local/webserver/nginx/sbin/nginx                      # 启动服务器
/usr/local/webserver/nginx/sbin/nginx -s reload            # 重新载入配置文件
/usr/local/webserver/nginx/sbin/nginx -s reopen            # 重启 Nginx
/usr/local/webserver/nginx/sbin/nginx -s stop              # 停止 Nginx

View process:

ps -ef | grep nginx #查看Nginx进程

Conclusion:

This configuration file name I toss most of the day also tried ip domain hosts have also tried to set off in the browser but is not accessible through the domain name
last used ip access success is a success but certainly can not do so otherwise the domain name white bought a
preliminary estimate of the problem may be configured domain name is not mapped successfully led the hour is late tomorrow look and see
Here Insert Picture Description


Published 174 original articles · won praise 5 · Views 240,000 +

Guess you like

Origin blog.csdn.net/Piconjo/article/details/104980268