博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Nginx 配置
阅读量:6894 次
发布时间:2019-06-27

本文共 3336 字,大约阅读时间需要 11 分钟。

Nginx是一个异步框架的Web服务器,也可以用作反向代理,负载平衡器 和 HTTP缓存。该软件由Igor Sysoev 创建,并于2004年首次公开发布。 同名公司成立于2011年,以提供支持。 Nginx是一款免费的开源软件,根据类BSD许可证的条款发布。

虽说 Nginx 一般都是由后端来配置的,但是如果你想成为一个全栈或者一个有追求的前端的话,了解一下 Nginx 还是很有必要的。如果没有服务器的话,自己可以装一个 Linux 虚拟机练下手。这里我用的 CentOS7 64位的系统来做演示。

虚拟机安装

这篇文章主要是介绍 Nginx 的,关于 Mac 安装 CentOS 虚拟机的教程可以参考这篇文章 ,其他系统就自行查找了。安装好之后可以输入 ping baidu.com 测试一下网络是不是正常的。

SSH 登录虚拟机

虚拟机安装好了之后我一般会用 terminal 连接虚拟机,因为想粘贴一些长命令时不是很好操作。使用命令 ssh rootName@ip,如下图所示

获取虚拟机的 IP 地址,可以在 VMware 的窗口输入 ifconfig,如果提示 ifconfig command not found,可以参考这篇文章

安装 Nginx

Nginx 安装也十分简单,输入 yum install -y nginx,如果无法安装,则先执行这条命令 rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm 安装 Nginx 源。然后再输入 yum install -y nginx。安装好之后可以输入 whereis nginx 查看 Nginx 的默认目录。输入 nginx 启动服务,然后在浏览器输入虚拟机的 IP 地址进行访问,如果无法访问的话,应该就是虚拟机的防火墙没有开启80端口,输入 firewall-cmd --permanent --add-port=80/tcp 开启防火墙的80端口,再输入 firewall-cmd --reload。到这里再访问一下虚拟机的 IP 地址,就可以看到 Nginx 的欢迎页面了。参考文章。

修改配置文件

输入 cd /etc/nginx 进入 /etc/nginx 目录,用 ls 命令可以看到 nginx.conf 这个文件。这个就是 nginx 的配置文件了。用 cat nginx.conf 命令来看下里面写了什么东西。内容大致如下

user  nginx;worker_processes  1;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.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;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    include /etc/nginx/conf.d/*.conf;}复制代码

主要配置内容还是在之后一句 include /etc/nginx/conf.d/*.conf; 接下来在进入 /etc/nginx/conf.d/ 目录看下有什么东西。同样用 ls 命令可以看到一个 default.conf 文件,内容大致如下

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;    # 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; #}}复制代码

可以看到这个服务在监听 80 端口,root 指向项目根目录,即我们访问 localhost (默认为80端口)时,将会查找 /usr/share/nginx/html 这个目录下的文件。我自己的配置一般如下

server {    listen 80;    server_name domain; #使用域名或 IP 地址    charset utf8; #字符编码    access_log /path/folder/access.log main; #访问日志    error_log /path/folder/error.log error; #错误日志    root /path/folder;    index index.html;}复制代码

日志文件需要我们到对应目录下创建对应的 log 文件,修改完配置文件可以使用 nginx -s reload 重启服务。

转载于:https://juejin.im/post/5acdb76c51882555784e5113

你可能感兴趣的文章
华章11-12月份新书简介(2017年)
查看>>
第三周作业
查看>>
Vector、ArrayList、List使用深入剖析
查看>>
【调试】Core Dump是什么?Linux下如何正确永久开启?
查看>>
新浪微博API授权
查看>>
电子政务网中信息共享机制的重要性
查看>>
Tomcat_本地项目host配置Server.xml
查看>>
[转载] 财经郎眼20120423:长点心吧“两桶油”!
查看>>
ZooKeeper源码研究系列 客户端创建连接过程分析
查看>>
MySQL datetime && timestamp
查看>>
vNet-vNet Site-to-Site ××× Connecting
查看>>
【Visual C++】游戏开发笔记十四 游戏画面绘图(四) 华丽的CImage类
查看>>
Struts2的配置
查看>>
Java基础学习总结(17)——线程
查看>>
招聘单位某些不可说的规则,你必须知道
查看>>
MDaemon v13新增特色功能
查看>>
纯JS实现图片验证码功能并兼容IE6-8
查看>>
Microsoft System Center 2012 Virtual Machine Manager 部署过程
查看>>
我的友情链接
查看>>
对话框按钮简单使用实例
查看>>