Centos7 yum nginx source installation and configuration describes nginx

Centos7 yum nginx source installation and configuration describes nginx

(1) Installation

1.1 Add Nginx repository
yum install epel-release
1.2 start the installation nginx
yum install nginx

The middle of these two commands will prompt you whether you just need to continue all the way yyyyyyyyyyy

Here nginx already installed

1.3nginx start caveats

Whether 1.nginx port is occupied, nginx default port is port 80

2. 80-port server is turned on protective wall and open the corresponding security group

80 under the command open firewall port, a cloud server, then set up security groups need to be logged Ali cloud or cloud Tencent open security group

firewall-cmd --zone=public --add-port=80/tcp --permanent

Try to start! !

Nginx comes with commonly used commands

#启动nginx  两种命令都可以启动
systemctl start nginx   
nginx

#设置nginx开机启动
systemctl enable nginx

#卸载nginx命令
yum remove nginx

#停止nginx
nginx -s stop

#重启nginx
nginx -s reload

If you do not modify the default port nginx nginx start direct access to the server Ip (80 default can not write) can, if you modify the default port access method for the server ip: nginx port

After visiting the page appears at the prompt explanation nginx has been successfully installed and started

Welcome to CentOS
The Community ENTerprise Operating System

(2) nginx Configuration

Using the default configuration nginx source installed position yum

/etc/nginx/nginx.conf
# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/
#运行用户
user nginx;
#启动进程,每个Nginx进程平均耗费10M~12M内存。建议指定和CPU的数量一致即可
worker_processes auto;
#全局错误日志及PID文件
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
#工作模式
events {
	#单个后台worker process进程的最大并发链接数
    worker_connections 1024;
}
#http服务器,负载均衡以及反向代理都是使用它
http {
    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;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;
    
	#设定mime类型,类型由mime.type文件定义
    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;
    
    #server 块是对虚拟主机的配置,server标志定义虚拟主机开始
    server {
    	#listen用于指定虚拟主机的服务端口,启动后就是虚拟机ip+此指定的端口进行访问
        listen       80 default_server;
        listen       [::]:80 default_server;
        #server_name 用来指定IP地址或域名,多个域名之间用空格分开
        server_name  _;
        #root指令用于指定虚拟主机的网页根目录,这个目录可以是相对路径,也可以是绝对路径
        root         /usr/share/nginx/html;

        #加载默认服务器组的配置文件.
        include /etc/nginx/default.d/*.conf;
		#location块
		#URL地址匹配 /a  访问就ip:端口/a 
		#下方location是我自己目前的配置
		#在一个server中 可以配置多个location ,但是“/“必须有一个
        location / {
        #ftp文件主目录,只允许有一个
        root /var/ftp/pub/;
        #此访问的首页
        index index.html index.htm index.php index.jpg index.mp4 index.txt;
        }
        #404页
        error_page 404 /404.html;
            location = /40x.html {
        }
		#500页
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
        #第二个location
        location /chexuan/ {
        alias /ftp/chexuan/;
        index index.html index.htm index.php index.jpg index.mp4 index.txt;
        }

        #举例
        #访根目录/, 比如http://localhost/ 就会匹配 /var/ftp/pub/下的index文件
        #访问目录/chexuan/  比如http://localhost/chexuan/ 就会匹配 /ftp/chexuan/下的index文件
    }

# Settings for a TLS enabled server.
#
#    server {
#        listen       443 ssl http2 default_server;
#        listen       [::]:443 ssl http2 default_server;
#        server_name  _;
#        root         /usr/share/nginx/html;
#
#        ssl_certificate "/etc/pki/nginx/server.crt";
#        ssl_certificate_key "/etc/pki/nginx/private/server.key";
#        ssl_session_cache shared:SSL:1m;
#        ssl_session_timeout  10m;
#        ssl_ciphers HIGH:!aNULL:!MD5;
#        ssl_prefer_server_ciphers on;
#
#        # Load configuration files for the default server block.
#        include /etc/nginx/default.d/*.conf;
#
#        location / {
#        }
#
#        error_page 404 /404.html;
#            location = /40x.html {
#        }
#
#        error_page 500 502 503 504 /50x.html;
#            location = /50x.html {
#        }
#    }

}


Wherein the difference between the root and the alias location code block

root实例:

location /t/ {
     root /www/root/html/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/t/a.html的文件。

alias实例:

location /t/ {
 alias /www/root/html/leilei/;
}
如果一个请求的URI是/t/a.html时,web服务器将会返回服务器上的/www/root/html/leilei/a.html的文件。注意这里是leilei,因为alias会把location后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。


Note :

  1. When using the alias, the directory name must be added "/."
  2. alias when using a regular match, must capture content to be matched and used at the specified content.
  3. alias can only be placed in the location block. (Root can not be placed in the location)

Nginx is currently learning, and, to this end-depth follow-up time continue to update

Published 29 original articles · won praise 22 · views 3487

Guess you like

Origin blog.csdn.net/leilei1366615/article/details/104055134
Recommended