nginx配置文件

96 阅读2分钟
nginx搭建之前需要有云服务器,并且得清楚属于哪一种操作系统(CentOS,ubuntu),不同的操作系统下使用的包管理器不同

image image

一、前期了解

1、连接服务器的三种方式
  1. 云服务器自己提供了远程连接的方式连接方式简单,直接在网页上操作
  2. 本地命令连接可操作范围更广
  3. 客户端工具连接(ForkLift mac推荐,Transmit mac推荐)可视化界面,操作简单,操作范围有局限
注意:此处使用本地命令方式、CentOS操作系统
2、使用本地命令须知操作的命令
退出服务器:exit
清除掉终端当前的操作记录:clear
以前用过此服务器,但是又修改过该服务器就会出现连接报错解决:ssh-keygen -R 公网ip
启动nginx:nginx
停止nginx:nginx -s stop
重启nginx:nginx -s reload
使用指定配置文件启动nginx:nginx -c 文件路径
检查nginx配置文件:nginx -t
查看nginx版本信息:nginx -v
查看所有端口占用情况:netstat -antp | grep :
杀掉全部nginx进程:killall -9 nginx
3、nginx常见目录
使用 whereis nginx 命令可以查看关于nginx的目录
常用文件目录:
执行目录:/usr/sbin/nginx
模块所在目录:/usr/lib64/nginx/modules
配置文件:/etc/nginx/nginx.conf
日志文件:/var/log/nginx
虚拟主机目录:/usr/share/nginx/html

二、nginx环境搭建

1、准备工作
1、命令行连接服务器
ssh root@公网ip
2、安装nginx
yum install nginx
3、查看安装nginx是否成功
ls -l /etc/nginx
2、nginx.conf主配置文件解析
#运行nginx的用户
user nginx;

#工作进程数量,建议不要修改,保持自动
worker_processes auto;

#错误日志文件的输出位置和输出级别
error_log /var/log/nginx/error.log;

#pid文件位置
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
	#每个进程的最大连接数,默认是1024
    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函数来输出文件
    sendfile            on;

	#是否启用nodelay算法
    tcp_nopush          on;
    tcp_nodelay         on;

	#连接超时时间
    keepalive_timeout   65;
    types_hash_max_size 2048;
	
	#支持的文件类型
    include             /etc/nginx/mime.types;

	#默认的文件类型
    default_type        application/octet-stream;
	
	#从/etc/nginx/conf.d目录加载模块化配置文件
  
	#引入外部配置文件,包含虚拟主机配置
    include /etc/nginx/conf.d/*.conf;
	#以下为虚拟主机配置
    server {
		#监听的端口80
        listen       80 default_server;
        listen       [::]:80 default_server;

		#主机域名
        server_name  _;
		#默认页面路径
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
		
		#404页面
        error_page 404 /404.html;
            location = /40x.html {
        }
		
		#错误的反馈页面
        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}
3、自定义搭建nginx.conf文件

修改改文件推荐使用ForkLift mac工具更方便

#user  nobody; 
worker_processes  1; 

 
#pid        logs/nginx.pid; 
 
 
events { 
    worker_connections  1024; 
} 
 
 
http { 
    include       mime.types; 
    default_type  application/octet-stream; 
    client_max_body_size 200m;
    sendfile        on;
    keepalive_timeout  65; 
    server { 
        listen       80; 
        server_name  localhost;
        location / { 
            root	html; 
            index	index.html index.htm; 
        }
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
            root   html; 
        }
    }

	server { 
        listen       8090; 
        server_name  localhost;
		
        #access_log  logs/host.access.log; 
        location / { 
			root         /usr/share/nginx/html; 
			index 		index.html; 
			#try_files $uri $uri/ /index.html; 
        } 
		
		location /img {
			root /usr/share/nginx/html/;
			autoindex on;
		}
        error_page   500 502 503 504  /50x.html; 
        location = /50x.html { 
			root   html; 
        } 
    }
} 

注意:

  1. 、此时已经可以使用公网ip访问html页面了
  2. 在浏览器url输入公网ip回车访问的是默认端口80
  3. 在公网ip后加上端口8090访问的是服务端指定的路径/usr/share/nginx/html下的index.html页面,所以可以根据配置不同的端口访问不同路径页面
  4. 访问此时/usr/share/nginx/html/images/路径下的图片,url输入公网:ip:8090/images/IMG_1.jpg即可访问,说明此时:“公网ip:8090/”访问的是服务器的“/usr/share/nginx/html/”此路径,后加“images/IMG_1.jpg”表示访问到服务器指定的images文件夹下指定图片
  5. 每次修改nginx.conf文件后需要重启nginx,否则访问无效