centos7通过yum安装nginx以及其简单配置使用

干货!干货!干货!

执行命令:

$ yum install -y nginx

通过yum安装的时候提示下面的错误:

[root@localhost ~]# yum install nginx
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
没有可用软件包 nginx。
错误:无须任何处理

添加nginx的源:

$ rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

该命令执行之后,会在/etc/yum.respos.d下面多出一个nginx.repo,如下:

[root@localhost ~]# ll
总用量 40
-rw-r--r--. 1 root root 1572 12月  1 2016 CentOS-Base.repo
-rw-r--r--. 1 root root 1572 12月  1 2016 CentOS-Base.repo.backup
-rw-r--r--. 1 root root 1664 10月 24 10:36 CentOS-Base.repo.bak
-rw-r--r--. 1 root root 1309 8月  30 2017 CentOS-CR.repo
-rw-r--r--. 1 root root  649 8月  30 2017 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 8月  30 2017 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 8月  30 2017 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 8月  30 2017 CentOS-Sources.repo
-rw-r--r--. 1 root root 3830 8月  30 2017 CentOS-Vault.repo
-rw-r--r--. 1 root root  113 7月  15 2014 nginx.repo

然后再执行安装命令:

$ yum install -y nginx

安装之后,可以查看nginx的默认安装目录:

$ whereis nginx

以下是Nginx的默认路径:

(1) Nginx配置路径:/etc/nginx/

(2) PID目录:/var/run/nginx.pid

(3) 错误日志:/var/log/nginx/error.log

(4) 访问日志:/var/log/nginx/access.log

(5) 默认站点目录:/usr/share/nginx/html

事实上,只需知道Nginx配置路径,其他路径均可在/etc/nginx/nginx.conf 以及/etc/nginx/conf.d/default.conf 中查询到

nginx相关的验证命令及启动命令:

$ nginx   #启动
$ nginx -t  #测试命令
$ nginx -s relaod #修改nginx.conf之后,可以重载

在nginx.conf中,在server下添加以下内容:

root         /home/用户名/应用所在的文件夹;    #root         /home/ossuper/web;
include /etc/nginx/default.d/*.conf;

location / {
    index index.html;
}

#例如请求路径为:http://ip:port/api 
location /api {   #请求的路径
    proxy_pass http://<:ip>:<:port>;    #请求url
    proxy_redirect off;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-proto https;
}

以上是配置简单的http请求,https的需要证书,因为自己了解的也不多,所以今后有时间再更新。

注:本文纯属自己在学习过程中的一些总结,欢迎各位提出建议,谢谢!

发布了14 篇原创文章 · 获赞 23 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/Jaiaxn/article/details/82910623
今日推荐