Nginx初步学习

1,217 阅读1分钟

注意事项

  1. 环境: centos7, nginx1.1.6
  2. 在Windows和win10上的linux中做测试,发现配置总也不能生效,而且还少一些必要的文件,所以不建议在Windows和win10上的linux中学习.

安装

  1. yum安装必要的程序
yum -y install gcc gcc-c++ autoconf pcre-devel make automake
yum -y install wget httpd-tools vim
  1. 配置yum源

vim /etc/yum.repos.d/nginx.repo

[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/7/$basearch/
gpgcheck=0
enabled=1
  1. 安装nginx
yum install nginx
nginx -v

nginx配置文件

/etc/nginx/nginx.conf

#运行用户,默认即是nginx,可以不进行设置
user  nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes  1;   
#错误日志存放目录
error_log  /var/log/nginx/error.log warn;
#进程pid存放位置
pid        /var/run/nginx.pid;


events {
    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;   #nginx访问日志存放位置

    sendfile        on;   #开启高效传输模式
    #tcp_nopush     on;    #减少网络报文段的数量

    keepalive_timeout  65;  #保持连接的时间,也叫超时时间

    #gzip  on;  #开启gzip压缩

    include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件

/etc/conf.d/default.conf

server {
    listen       80;   #配置监听端口
    server_name  localhost;  //配置域名

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

    location / {
        root   /usr/share/nginx/html;     #服务默认启动目录
        index  index.html index.htm;    #默认访问文件
    }

    #error_page  404              /404.html;   # 配置404页面

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

nginx相关命令

  1. nginx启动
方式1: 
nginx

方式2: 
systemctl start nginx.service
  1. nginx停止
(1) 从容停止服务
nginx -s quit

(2) 立即停止服务
nginx  -s stop

(3) 杀死进程
killall nginx

(4) 服务命令停止
systemctl stop nginx.service
  1. nginx重启
(1) 重新载入配置文件
nginx -s reload
(2) 服务方式停止
systemctl restart nginx.service

nginx设置虚拟主机

  1. 基于端口号设置虚拟主机
  • nginx默认监听80端口,此处通过监听8001端口,服务启动目录指向/usr/share/nginx/html/html8001,当用户访问http://localhost:8001时,会访问/usr/share/nginx/html/html8001下的index.html.
  • 通过编写多个server,达到多个端口号设置虚拟主机的效果
server{
        listen 8001;
        server_name localhost;
        root /usr/share/nginx/html/html8001;
        index index.html;
}
  1. 基于ip/域名设置虚拟主机
  • 类似于用端口设置虚拟主机,此处使用server_name来设置不同的虚拟主机
  • 通过编写多个server,达到多个ip/域名设置虚拟主机的效果
server{
        listen 80;
        server_name 112.74.164.244;
        root /usr/share/nginx/html/html8001;
        index index.html;
}

nginx反向代理

下面代码意思是监听80端口,当用户访问nginx2.jspang.com时,将用户访问代理到http://jspang.com域名下.

server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
               proxy_pass http://jspang.com;
        }
}

其他反向代理命令

  • proxy_set_header :在将客户端请求发送给后端服务器之前,更改来自客户端的请求头信息。

  • proxy_connect_timeout:配置Nginx与后端代理服务器尝试建立连接的超时时间。

  • proxy_read_timeout : 配置Nginx向后端服务器组发出read请求后,等待相应的超时时间。

  • proxy_send_timeout:配置Nginx向后端服务器组发出write请求后,等待相应的超时时间。

  • proxy_redirect :用于修改后端服务器返回的响应头中的Location和Refresh。

$http_user_agent使用

  • if (http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') 的意思是如果在http_user_agent存在(Android|webOS|iPhone|iPod|BlackBerry),则认为是手机访问,将项目启动根目录设置到手机部署目录
  • 在开发中,可能会将pc和phone分开为不同的域名,如pc: www.xxx.com; phone: m.xxx.com; 此时应该通过proxy_pass来实现需求.
server{
        listen 80;
        server_name nginx2.jspang.com;
        location / {
         root /usr/share/nginx/pc;
         if ($http_user_agent ~* '(Android|webOS|iPhone|iPod|BlackBerry)') {
            root /usr/share/nginx/mobile;
         }
         index index.html;
        }
}