linux nginx 安装 及 配置

Posted guxiaohai_

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux nginx 安装 及 配置相关的知识,希望对你有一定的参考价值。

一:下载nginx(下载可能会有点慢,耐心等待)
官网地址:http://nginx.org/en/download.html

二:解压并安装

  1. 安装依赖(安装过的跳过)
yum -y install gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel open openssl-devel
  1. 导入并解压:tar -zxvf nginx-1.20.2.tar.gz
  2. 编译执行configure文件
# 不用https执行指令
./configure  --with-http_ssl_module
# 需要使用https执行
./configure 
  1. 编译执行make命令
make 
make install
  1. 创建日志文件
mkdir ./usr/local/nginx/logs

三:配置nginx.conf

user root;
worker_processes auto;

# 错误日志
error_log logs/error.log;

#nginx.pid文件
pid logs/nginx.pid;

events 
    worker_connections 1024;


http 
    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;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             mime.types;
    default_type        application/octet-stream;
	
    server 
        listen       80;
        listen       [::]:80;
        server_name  test.cn;
        location / 
            index  index.html;
            alias   /usr/test/;
            autoindex on;
        
        error_page 404 /404.html;
    

    server 
        listen       80;
        listen       [::]:80;
        server_name  test.cn;

        ssl_certificate      /test.cn.pem;
        ssl_certificate_key  /test.cn.key;

        location / 
            proxy_pass http://localhost:8801/;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Real-IP $remote_addr;
            # 将此路由请求的实体大小限制为20m
            client_max_body_size 20m;
        

        error_page 404 /404.html;
    
	
    server 
        listen       443 ssl;
        server_name  test.cn;

		#证书文件
        ssl_certificate      /test.cn.pem;
        ssl_certificate_key  /test.cn.key;

        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;

        ssl_prefer_server_ciphers  on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers ECDH:AESGCM:HIGH:!RC4:!DH:!MD5:!aNULL:!eNULL;

        location / 
            proxy_pass http://localhost:8801/;
            proxy_http_version 1.1;
            proxy_set_header   Upgrade $http_upgrade;
            proxy_set_header   Connection keep-alive;
            proxy_set_header   Host $host;
            proxy_cache_bypass $http_upgrade;
            proxy_set_header   X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header   X-Forwarded-Proto $scheme;
            proxy_set_header   X-Real-IP $remote_addr;
            # 将此路由请求的实体大小限制为20m
            client_max_body_size 20m;
        
        error_page 404 /404.html;
    
	

四:启动/停止

  1. 启动
cd /usr/local/nginx/sbin
# 默认配置文件启动
./nginx
# 指定配置文件启动
./nginx -c  /usr/local/nginx/conf/nginx.conf
  1. 停止
cd /usr/local/nginx/sbin
# 停止指令
./nginx -s stop

以上是关于linux nginx 安装 及 配置的主要内容,如果未能解决你的问题,请参考以下文章

linux nginx 安装 及 配置

linux——nginx的安装及配置

Nginx安装与配置

linux服务之nginx的安装及配置(centos)

linux 安装 nginx 及反向代理配置

Linux 安装及配置 Nginx + ftp 服务器