Nginx配置教程

Nginx 使用教程

安装编译工具及库文件

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

安装 PCRE

PCRE库支持正则表达式。如果配置文件nginx.conf中使用了正则表达式,那么在编译Nginx时就必须把PCRE库编译进Nginx,因为Nginx的HTTP模块需要靠它来解析正则表达式。另外,pcre-devel是使用PCRE做二次开发时所需要的开发库,包括头文件等,这也是编译Nginx所必须使用的。

下载 PCRE 安装包

  1. 我是使用xftp传输到服务器目录/usr/local/src/
  2. 解压安装包
    • tar zxvf pcre-8.35.tar.gz
  3. 进入安装目录,编译安装
    • cd pcre-8.35
    • ./configure
    • make && make install
  4. 查看pcre版本
    • pcre-config –version

安装 Nginx

  1. Nginx官网下载
  2. 传输到服务器目录/usr/local/src/
  3. 解压安装包
    • tar zxvf nginx-1.16.1.tar.gz
  4. 进入安装目录,编译安装
cd nginx-1.16.1
./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
  1. 查看版本
    • /usr/local/webserver/nginx/sbin/nginx -v
  2. 软连接,做全局命令
    • ln -s /usr/local/webserver/nginx/sbin/nginx /usr/local/bin/

Nginx 配置

  1. 配置nginx.conf ,修改/usr/local/webserver/nginx/conf/nginx.conf
user nobody;
worker_processes  1;

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

#pid        /usr/local/webserver/nginx/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;
#gzip压缩功能设置
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 6;
gzip_types text/plain text/css text/javascript application/json application/javascript application/x-javascript application/xml;
gzip_vary on;

#http_proxy 设置
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 75;
proxy_send_timeout 75;
proxy_read_timeout 75;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_temp_path /usr/local/webserver/nginx/proxy_temp 1 2;
#下面是server虚拟主机的配置
server
  {
    listen 80;#监听端口
    server_name admin.haojie365.cn; #你的域名
  location / {
    proxy_pass http://localhost:8888; #转发的地址端口
    #允许cros跨域访问
    add_header 'Access-Control-Allow-Origin' '*';
  }
  }
  server
  {
    listen 80;#监听端口
    server_name play.haojie365.cn;#域名
  root   /home/server/testnode/admin; #项目的目录地址
  index  index.html index.htm;
  }
}

启动 Nginx

  1. 检查配置文件nginx.conf的正确性
    • nginx -t
  2. 启动nginx命令
    • nginx
  3. 从浏览器访问域名,看到welcome to nginx,就是配置成功
  4. 其他命令
    • nginx -s reload # 重新载入配置文件
    • nginx -s reopen # 重启
    • nginx -s stop # 停止

Nginx启动成功,却访问不了

  1. 先查看nginx配置是否正确
    • nginx -t
  2. 查看nginx是否启动成功
    • ps -ef | grep nginx #查看nginx端口
  3. 如果是云服务器,配置服务器安全组,放开对应的端口
  4. 检查服务器对应的端口是否放开
  5. 检查防火墙是否开启,放开防火墙的端口
    • /sbin/iptables -I INPUT -p tcp –dport 8011 -j ACCEPT #开启8011端口
    • /etc/rc.d/init.d/iptables save #保存配置
    • /etc/rc.d/init.d/iptables restart #重启服务
    • /etc/init.d/iptables status #查看端口是否已经开放
  6. 查看端口是否放开
    • telnet ip port

文章作者: Hao Jie
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Hao Jie !
  目录