Nginx初步学习

注意事项

  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.repophp

[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.confhtml

#运行用户,默认便是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.conflinux

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域名下.nginx

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

其余反向代理命令c++

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

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

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

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

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

$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;
        }
}
复制代码
相关文章
相关标签/搜索