Nginx_安装配置及常用命令

Nginx启动关闭命令:

启动Nginx:

# cd /usr/local/nginx/sbin

# ./nginx

关闭nginx:

强制关闭: # ./nginx -s stop

快速关闭: # ./nginx -s quit

Nginx快速安装:

1. 安装 gcc

# yum install gcc-c++

2. 安装 PCRE pcre-devel 

# yum install -y pcre pcre-devel

3. 安装 zlib

# yum install -y zlib zlib-devel

4. 安装 OpenSSL

# yum install -y openssl openssl-devel

5. 查看最新版下载地址 (一般使用 Stable Version 稳定版)

https://nginx.org/en/download.html

6. 通过下载地址下载文件

# wget https://nginx.org/download/nginx-1.13.6.tar.gz

7. 解压文件

# tar -zxvf nginx-1.10.1.tar.gz

8. 进入解压后的文件目录

# cd nginx-1.13.6

9.执行配置. 

SSL证书模块, HTTP2 模块 (建议选这个, 模块比较齐全)

# ./configure --prefix=/usr/local/nginx --with-pcre --with-stream --with-stream_ssl_module --with-http_stub_status_module --with-http_ssl_module --with-http_v2_module --with-threads

安装时附带SSL证书模块, 如果没有ssl不能配置HTTPS

# ./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

10. 编译文件

# make

11.安装文件

# make install

12.启动nginx

# cd /usr/local/nginx/sbin/

# ./nginx
         

13.制作一个软连接方便使用:

# ln -s /usr/local/nginx ./nginx

Nginx配置文件详解:

配置HTTPS 以及 HTTP 2.0

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    # 设置上传文件大小
    client_max_body_size 500m;
    
    # 配置Buffer大小
    proxy_buffer_size 128k;
    proxy_buffers   32 128k;
    proxy_busy_buffers_size 128k;

    # gzip  on;
    # 配置缓存.
    proxy_temp_path /mnt/nginx_temp;
    proxy_cache_path /mnt/nginx_cache levels=1:2 keys_zone=cache_one:200m inactive=5d max_size=400m;
    proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;


    # 配置大小
	proxy_buffer_size 128k;
	proxy_buffers   32 128k;
	proxy_busy_buffers_size 128k;

	server {
		listen 80;
		server_name www.brando.cn admin.brando.cn;	#访问域名
		rewrite ^(.*)$  https://$host$1 permanent;
	}
	server {
		listen 443 ssl http2;
		server_name www.brando.cn admin.brando.cn;
		
		ssl on;
		ssl_certificate /alidata/server/ssl/brando.cn.crt;	#ssl文件目录
		ssl_certificate_key /alidata/server/ssl/brando.cn.key;	#ssl文件目录

		#ssl优化配置.
		ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
		ssl_stapling on;
		ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA";
		ssl_prefer_server_ciphers on;
		ssl_session_cache shared:SSL:50m;
		ssl_session_timeout 10m;

		location / {
			proxy_set_header   X-Real-IP $remote_addr;
            proxy_set_header   X-Forwarded-Proto $scheme;
			proxy_set_header   Host      $http_host;
			proxy_pass         http://127.0.0.1:8080;
			proxy_connect_timeout 1800s;
			proxy_send_timeout 1800s;
			proxy_read_timeout 1800s;
		}
	}
}

Nginx代理目录:

location /resources/ {
	alias /alidata/server/www/;
	if ($arg_docName ~* \.*$) {
		add_header Content-Disposition "attachment;filename=$arg_docName";
		#配置跨域
		add_header Access-Control-Allow-Origin *;
		add_header Access-Control-Allow-Headers X-Requested-With;
		add_header Access-Control-Allow-Methods GET,POST,OPTIONS;
	}
}

Nginx配置文件模板:

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值