Centos7 냠의 nginx 소스 설치 및 구성의 nginx 설명

Centos7 냠의 nginx 소스 설치 및 구성의 nginx 설명

(1) 설치

1.1 추가 Nginx의 저장소
yum install epel-release
1.2 설치의 nginx를 시작합니다
yum install nginx

이 두 명령의 중간 방금 모든 방법 yyyyyyyyyyy을 계속해야하는지 여부를 묻는 메시지가 표시됩니다

여기에 nginx를 이미 설치

주의 시작 1.3nginx

1.nginx 포트가 점유 여부, nginx를 기본 포트는 포트 80

2. 80 포트 서버 보호벽 개방 대응 보안 그룹 켜져

명령 열려있는 방화벽 포트, 클라우드 서버 아래 (80)는 다음 보안 그룹이 알리 구름 또는 구름 텐센트 개방 보안 그룹 로그인해야 설정

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

시작해보십시오! !

Nginx에 일반적으로 사용되는 명령과 함께 제공

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

#设置nginx开机启动
systemctl enable nginx

#卸载nginx命令
yum remove nginx

#停止nginx
nginx -s stop

#重启nginx
nginx -s reload

기본 포트를 수정하지 않으면 nginx를 nginx를 서버 IP로 직접 액세스를 시작합니다 (80 기본값은 쓸 수 없습니다) 수, 서버의 IP에 대한 기본 포트 액세스 방법을 수정하는 경우 : nginx를 포트를

프롬프트 설명의 nginx에서 페이지가 나타납니다를 방문 후 성공적으로 설치 및 시작되었습니다

Welcome to CentOS
The Community ENTerprise Operating System

(2) 구성의 nginx

냠 위치를 설치 한 기본 구성의 nginx 소스를 사용하여

/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 {
#        }
#    }

}


상기 루트 별명 위치 코드 블록 사이의 차이

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后面配置的路径丢弃掉,把当前匹配到的目录指向到指定的目录。


참고 :

  1. 별칭을 사용하는 경우, 디렉토리 이름을 추가해야합니다 "/."
  2. 정규 일치를 사용하는 경우, 내용을 캡처해야 별칭이 일치하고 지정된 내용에 사용할 수 있습니다.
  3. 별명은 블록 위치에 배치 될 수있다. (루트는 위치에 배치 할 수 없습니다)

Nginx는 현재 계속 업데이트이를 심층적 인 후속 시간, 학습하고,

게시 29 개 원래 기사 · 원의 찬양 (22) · 전망 3487

추천

출처blog.csdn.net/leilei1366615/article/details/104055134