Getting Started Configuring nginx

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/nightwishh/article/details/102596571

This server with Ali cloud Centos7

nginx configuration file structure

After installing nginx we cd into the / etc / directory under nginx, and there are two more important configuration files.
nginx.conf master profile is nginx, conf.d configuration file for the sub nginx
Here Insert Picture Description

	接着我们输入命令vim nginx.conf进入nginx.conf配置文件
  • nginx.conf configuration file as follows (# is back here first read through the explanatory notes to understand)
user  nginx; 
#用户名称,默认nginx,可以不设置

worker_processes  1;   
#Nginx进程数量 设置和cpu一样即可

error_log  /var/log/nginx/error.log warn;  
#错误日志存放目录
#如遇服务启动失败可cd到此目录用命令tail error.log输出此错误日志

pid        /var/run/nginx.pid;
#进程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; #包含的子配置项位置和文件
  • conf.d configuration file

      由于是文件夹,我们先cd进去再用vim编辑器进入此配置文件
    

Here Insert Picture Description

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;
    #}
}
安全组配置

Here we had the nginx can provide a service, then we need to set the security group configuration
If you are Ali cloud server, then cut to the console where the security group configuration and enter the configuration page rules
Here Insert Picture Description

添加安全组规则

Here Insert Picture Description
Port range here is 80/80, because we are in front of default.conf configured to listen on port 80
Here Insert Picture Description
Here Insert Picture Description
browser enter the server address output following description nginx configuration successful.
Here Insert Picture Description
Because the browser's default port 80 is listening, we can set it up to try to listen to other ports, such as 81
Here Insert Picture Description
after the set: wq to save and exit vim editor
and then restart nginx nginx -s reload command service
then remember Ali cloud the new console by rules to
Here Insert Picture Description
enter the server address output browser follows that we monitor a different port configuration is complete.
Here Insert Picture Description

Use the domain name set up virtual hosts

很简单还是在/etc/nginx/conf.d目录下vim进入default.conf配置文件修改server_name即可后面写的是你的域名
同样:wq保存退出编辑器然后nginx -s reload重启服务。访问域名即可出现welcome nginx的页面。
Here Insert Picture Description

nginx反向代理

  1. 代理

    代理简单来说,就是如果我们想做什么,但又想省去做事情中间的各种步骤和麻烦事,那么这时候就找一个中间人(代理服务器)帮我们达到想要的目的。
    Here Insert Picture Description

  2. 正向代理
    我们用过的vpn它就是正向代理,因为国内的ip地址是访问不了国外的网站的,这时vpn也就是代理服务器就给我们客户端做了一层代理,把我们的ip变成国外的ip那我们就能顺利访问了。
    Here Insert Picture Description
    像类似的还有游戏加速器等等,如果我们想玩外服的游戏通常也会用到它,通过转移修改节点使我们的延迟降低。
    Here Insert Picture Description

  3. 反向代理
    和正向代理最根本的区别就是:正向代理中是给客户端做代理。反向代理中是给服务器做代理。
    Here Insert Picture Description
    通过两张图最直观的表现就是,反向代理它只暴露了代理服务器,而真实的服务器其实是被隐藏的,服务器有很多台,客户端也不知道自己被分配到了哪台服务器中访问。
    接下来讲下配置

    反向代理的配置
    

    Or cd to enter default.conf /etc/nginx/conf.d under vim editor
    to modify the latter location address is your domain name
    Here Insert Picture Description
    Here Insert Picture Description
    so that the configuration is complete. Because we monitor in front of me is a subdomain http://nginx.heyaniu.com, then in the action here is to reverse proxy to http://heyaniu.com this site. After setting: wq command to save and then restart nginx nginx -s reload last open a browser to access the service http://nginx.heyaniu.com, you will find that we have successfully reverse proxy to http://heyaniu.com this page.

nginx load balancing

As the name suggests is to balance the load balancing server load balancing pressure, for example, there are 1000 users to access two servers, it would be best two visits each 500 which is load balancing.
Load Balancing configuration is also very simple.
Here Insert Picture Description
Configuration name just upstream from behind
where we configure two servers, weight behind the number indicates the amount of load.
Assume the same time there are 300 traffic, it will load the first 200 times a second load will be 100 times.

Guess you like

Origin blog.csdn.net/nightwishh/article/details/102596571