凌达干货 | nginx的安装及其配置文件详解
Part 01
nginx介绍
nginx....
Nginx (engine x) 是一个高性能的HTTP和反向代理web服务器,同时也提供了IMAP/POP3/SMTP服务。其特点是占有内存少,并发能力强,事实上nginx的并发能力在同类型的网页服务器中表现较好,中国大陆使用nginx网站用户有:百度、京东、新浪、网易、腾讯、淘宝等。(摘自百度百科)
Part 02
安装nginx
一、创建用户及用户组
nginx的工作进程需要一个用户的权限进行运 行,为了服务器安全,一般要指定一个普通用户权限的账号作为nginx的运行角色。在这我使用www用户作为nginx工作进程的用户,依次执行以下命令以创建用户及用户组
groupadd -r wwwuseradd -r -g www www
二、nginx编译安装
此处作为演示,安装的版本为1.16.1,若需要使用最新或者某个特定版本,可以到 nginx官网:http://nginx.org/download 在其列表中选择合适的版本,更改下列命令对应的版本号即可。依次执行以下命令进行nginx编译安装:
wget http://nginx.org/download/nginx--1.16.1.tar.gztar –zxvf nginx-1.16.1.tar.gz
三、将nginx加入至系统服务
为了便于日后对nginx执行启动等指令,需要将nginx加入至系统服务,依次输入以下内容:
vim /usr/lib/systemd/system/nginx.service#输入上面的指令后,添加下列内容[Unit]Description=nginx - high performance web serverDocumentation=http://nginx.org/en/docs/After=network.target remote-fs.target nss-lookup.target[Service]Type=forking# 路径对应安装路径Type=forkingExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.confExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.confExecReload=/usr/local/nginx/sbin/nginx -s reloadExecStop=/usr/local/nginx/sbin/nginx -s stopPrivateTmp=true[Install]WantedBy=multi-user.target
四、启动nginx
最后输入以下指令将nginx启动,并使得其可以开机自启:
systemctl start nginx.servicesystemctl enable nginx.service
五、安装完成
在浏览器访问这个服务器,若出现如下图的页面即为安装完成:
Part 03
nginx配置文件内容详解
#全局块:用于配置影响nginx全局的指令。user www; #配置用户或者组,默认为nobody nobody。worker_processes 1; #允许生成的进程数,默认为1#制定日志路径及级别#error_log logs/error.log;#error_log logs/error.log notice;#error_log logs/error.log info;#pid logs/nginx.pid; #指定nginx进程运行文件存放地址events {#events块:配置影响nginx服务器或与用户的网络连接。worker_connections 1024; #最大连接数,默认为512}http {#http块:可以嵌套多个server,配置代理,缓存,日志定义等绝大多数功能和第三方模块的配置。include mime.types; #嵌入文件扩展名与文件类型映射表配置default_type application/octet-stream; #默认文件类型,默认为text/plain#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 logs/access.log main; #日志格式设置sendfile on; #允许sendfile方式传输文件,默认为off#tcp_nopush on; #此选项允许或禁止使用socke的TCP_CORK的选项,此选项仅在使用sendfile的时候使用#keepalive_timeout 0;keepalive_timeout 65; #保持活跃时间(请求时间超过该值则返回超时)#gzip on; #响应报⽂压缩功能server {#server块:配置虚拟主机的相关参数,一个http中可以有多个server块。listen 80; #监听端口server_name localhost; #监听地址root /var/www; #根目录index index.html index.php; #设置默认页#charset koi8-r; #设置编码格式#access_log logs/host.access.log main; #指定日志文件的路径及日志格式。location / {#location块:配置请求的路由,以及各种页面的处理情况。root /var/www;index index.html index.php index.htm;}#error_page 404 /404.html; #错误页的配置# redirect server error pages to the static page /50x.html#error_page 500 502 503 504 /50x.html;location = /50x.html {root 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 /var/www;fastcgi_pass 127.0.0.1:9000; #Nginx通过本机的9000端口将PHP请求转发给PHP-FPM进行处理。fastcgi_index index.php;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; #等同于当前请求的root指令指定的值include fastcgi_params; #Nginx调用fastcgi接口处理PHP请求}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {# deny all;#}}}
学习心得:
能在工作室学到知识我感觉是十分充实的,虽然只是学了一些皮毛的皮毛,有时候学习一些内容花了许多时间去消化,也遇到了不少困难,但是我相信在未来,我在这个平台上能学到更多东西,使自己的技术不断接近工作室的各个大佬,自己能有一个更大的突破。
-END-
文案:曹桂源
排版:曹桂源
校对:Robinly
制作:凌达之珑达工作室
更多精彩
|||
