Linux环境安装合集(二)Nginx环境

本文提供nginx自定义路径和默认路径两种安装方式,并带有详细常用配置如https、代理等配置

直接yum安装

安装

输入yum list | grep nginx 可以查看yum版本

执行yum install nginx 安装完毕。

执行nginx -v 即可看到安装的版本

说明

路径 类型 作用
/etc/nginx/
etc/nginx/conf.d
/etc/nginx/conf.d/default.conf
/etc/nginx/nginx.conf
目录、配置文件 nginx主配置文件
/etc/logrotate.d/nginx 配置文件 nginx日志轮转,用于logrotate服务的日志切割
/etc/nginx/fastcgi_params
/etc/nginx/scgi_params
/etc/nginx/uwsgi_params
配置文件 cgi配置相关,fastcgi配置
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/win-utf
配置文件 编码转换映射转化文件(很少用到)
/etc/nginx/mime.types 配置文件 设置HTTP协议的Content-Type与拓展名对应关系
/etc/sysconfig/nginx
/etc/sysconfig/nginx-debug
/usr/lib/systemd/system/nginx-debug.service
/usr/lib/systemd/system/nginx.service
配置文件 用于配置出系统守护进程管理器的管理方式
/usr/lib64/nginx/modules
/etc/nginx/modules
目录 Nginx目录模块
/usr/sbin/nginx
/usr/sbin/nginx-debug
命令 Nginx服务的启动管理的终端命令
/usr/share/doc/nginx-1.15.0
/usr/share/doc/nginx-1.15.0/COPYRIGHT
/usr/share/man/man8/nginx.8.gz
文件、目录 Nginx的手册和帮助文件
/var/cache/nginx 目录 Nginx的缓存目录
/var/log/nginx 目录 Nginx的缓存目录

安装编译参数

nginx -V :会出现如下信息

-prefix=/etc/nginx
–sbin-path=/usr/sbin/nginx
–modules-path=/usr/lib64/nginx/modules
–conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error.log
安装目录或者路径
–http-client-body-temp-path=/var/cache/nginx/client_temp
–http-proxy-temp-path=/var/cache/nginx/proxy_temp
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp
–http-scgi-temp-path=/var/cache/nginx/scgi_temp
执行对应模块时,Nginx所保留的临时性文件
–user=nginx
--group=nginx
设定Nginx的进程启动的用户和组用户
–with-cc-opt C语言编译
–with-ld-opt= 设置附加的参数,链接系统库

进入主目录

cd /etc/nginx/

配置子配置文件

打开文件:vim /etc/nginx/conf.d/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /car/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;
    }
}
代码 含义
listen 80;
server_name localhost;
监听的端口
用域名方式访问的地址
location / { root /usr/share/nginx/html;
index index.html index.htm;
}
一个server里可以有多个location,当这是/的时候,
root是存放首页的路径访问的页面,访问index.html
error_page 500 502 503 504 404 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
当访问错误的时候,会显示的页面
root是存放页面的路径

自定义路径安装

gcc 安装

安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装:

yum install gcc-c++

PCRE pcre-devel 安装

PCRE(Perl Compatible Regular Expressions) 是一个Perl库,包括 perl 兼容的正则表达式库。nginx 的 http 模块使用 pcre 来解析正则表达式,所以需要在 linux 上安装 pcre 库,pcre-devel 是使用 pcre 开发的一个二次开发库。nginx也需要此库。命令:

yum install -y pcre pcre-devel

zlib 安装

zlib 库提供了很多种压缩和解压缩的方式, nginx 使用 zlib 对 http 包的内容进行 gzip ,所以需要在 Centos 上安装 zlib 库。

yum install -y zlib zlib-devel

OpenSSL 安装

OpenSSL 是一个强大的安全套接字层密码库,囊括主要的密码算法、常用的密钥和证书封装管理功能及 SSL 协议,并提供丰富的应用程序供测试或其它目的使用。
nginx 不仅支持 http 协议,还支持 https(即在ssl协议上传输http),所以需要在 Centos 安装 OpenSSL 库。

yum install -y openssl openssl-devel

文件下载

https://nginx.org/download/ 官网自行找版本

//下载
wget -c https://nginx.org/download/nginx-1.16.1.tar.gz
//解压
tar -zxvf nginx-1.16.1.tar.gz

配置

//进入目录配置
cd nginx-1.16.1
1.使用默认配置
./configure
2.自定义配置(不推荐)
./configure \
--prefix=/usr/local/nginx \
--with-http_stub_status_module
--conf-path=/usr/local/nginx/conf/nginx.conf \
--pid-path=/usr/local/nginx/conf/nginx.pid \
--lock-path=/var/lock/nginx.lock \
--error-log-path=/var/log/nginx/error.log \
--http-log-path=/var/log/nginx/access.log \
--with-http_gzip_static_module \
--http-client-body-temp-path=/var/temp/nginx/client \
--http-proxy-temp-path=/var/temp/nginx/proxy \
--http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
--http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
--http-scgi-temp-path=/var/temp/nginx/scgi
注:将临时文件目录指定为/var/temp/nginx,需要在/var下创建temp及nginx目录

安装

//执行安装
make install
//查看安装位置
whereis nginx

启动、停止

cd /usr/local/nginx/sbin/
./nginx 
./nginx -s stop
./nginx -s quit
./nginx -s reload
./nginx -s quit:此方式停止步骤是待nginx进程处理任务完毕进行停止。
./nginx -s stop:此方式相当于先查出nginx进程id再使用kill命令强制杀掉进程。

重启

1.先停止再启动(推荐):
对 nginx 进行重启相当于先停止再启动,即先执行停止命令再执行启动命令。如下:

./nginx -s quit
./nginx

2.重新加载配置文件:
当 ngin x的配置文件 nginx.conf 修改后,要想让配置生效需要重启 nginx,使用-s reload不用先停止 ngin x再启动 nginx 即可将配置信息在 nginx 中生效,如下:

./nginx -s reload

开机自启动

即在rc.local增加启动代码就可以了。

vi /etc/rc.local

增加一行 /usr/local/nginx/sbin/nginx
设置执行权限:

chmod 755 rc.local

设置代理

location / {
       proxy_pass http://www.xxx.com;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection "Upgrade";
       proxy_set_header Remote_addr $remote_addr;
       proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
       proxy_read_timeout 600s;
}

其他写法

listen 80;
server_name localhost;
index index.html index.htm index.php;
root  /usr/data/www;

#伪静态
location /{
    index index.php;
    if (!-e $request_filename) {
        rewrite  ^/(.*)$  /index.php/$1  last;
        break;
    }
}
location ~ \.php$ {
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}
#监控模块
location /nginx_status
{
	stub_status on;
	access_log   off;
}

location ~ .*\.(js|css)?$
{
	#缓存12天
	expires      12d;
}

location ~ /\.
{
	#禁止访问
	deny all;
	
}

配置所有路径显示一个页面

server {
        listen       80;
        server_name  www.xxx.com;
   location / {
		rewrite   /* /notice.html last;
	}
}

配置PHP

server {
        listen       80;
        server_name  www.xxx.com;

        location / {
            root           /data/php;
            fastcgi_pass   unix:/tmp/php-cgi.sock;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

 }

配置SSL

server {
		listen 443;
		server_name www.xxx.com; 
		ssl on;
		#证书文件路径
		ssl_certificate /data/ssl/*.pem;
		ssl_certificate_key /data/ssl/*.key;
		
		ssl_session_timeout 5m;
		ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_prefer_server_ciphers on;

		location /
		{
			proxy_pass http://127.0.0.1:8080;
			proxy_http_version 1.1;
			proxy_set_header X-Real-IP $remote_addr;
		}
		//配置二级路径 访问www.xxx.com/second  会进入
		location ^~ /second{
			proxy_pass http://127.0.0.1:18080/;
			proxy_set_header Host $proxy_host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_set_header Via "nginx";
		}
}

自定义地址别名

//配合使用
//放server外 自己定义
upstream zidingyi{ 
      server 127.0.0.1:8080;
}
//放server内
 server {
        listen       80;
        server_name  www.xxx.com;

        location / {
			proxy_pass http://zidingyi;
			proxy_send_timeout 600;
			proxy_read_timeout 600;
        }
    
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
			root   html;
        }   
    }

补充

location /img/ {
    alias /www/image/;
}
#若按照上述配置的话,则访问/img/目录里面的文件时,ningx会自动去/www/image/目录找文件
location /img/ {
    root /www/image;
}
#若按照这种配置的话,则访问/img/目录下的文件时,nginx会去/www/image/img/目录下找文件。

location /jd_root.txt {
    alias /www/jd_root.txt;
}
#或者
location /jd_root.txt {
    root /www/;
}

猜你喜欢

转载自blog.csdn.net/flaystar/article/details/107158758