Ubuntu 安装nginx

Posted 八戒

tags:

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

1、更新Ubuntu

# add-apt-repository 仓库名称
# apt-get update
# apt-get install -y libssl1.0.2 openssl

2、安装nginx

# apt-get install -y nginx
# apt-get install -y nginx-extras

3、设置Nginx通用配置文件

cat /etc/nginx/nginx.conf


user www-data; #ubuntu里面nginx用户名
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 1024;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    server_tokens off;   #关闭显示nginx版本

    server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##

    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;
    ssl_ciphers "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:EECDH+AESGCM:EDH+AESGCM:AES2
56+EECDH:AES256+EDH";
    ssl_ecdh_curve secp384r1; # Requires nginx >= 1.1.0
    ssl_session_cache shared:SSL:10m;
    ssl_session_tickets off; # Requires nginx >= 1.5.9
    # ssl_stapling on; # Requires nginx >= 1.3.7
    ssl_stapling_verify on; # Requires nginx => 1.3.7

    ##
    # Logging Settings
    ##

    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 /data/log/nginx/access.log main;
    error_log /data/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_vary on;
    gzip_min_length 1k;
    gzip_buffers 4 32k;
    gzip_disable "msie6";
    gzip_disable "MSIE [1-6].";
    gzip_http_version 1.1;
    gzip_comp_level 3;
    gzip_types text/plain application/x-javascript text/css application/xml text/javascript application/javascript application/json;
    ##
    # Proxy Headers
    ##
    include /etc/nginx/proxy.conf;   #设置代理头信息
    more_set_headers "Server: Customer Web Server Header";

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;  #配置各个站点的信息
}

4、设置Nginx代理

cat /etc/nginx/proxy.conf

proxy_redirect          off;
proxy_set_header        Host $host;
#proxy_set_header        X-Real-IP $remote_addr; #获取真实IP
proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

5、设置站点配置信息

cd /etc/nginx/sites-enabled/

upstream local_iis_80 { server 后端IP1:
80 max_fails=2 fail_timeout=10s weight=10; server 后端IP2:80 max_fails=2 fail_timeout=10s weight=10; keepalive 45; #防止出现nginx向后端发请求的时候连接打爆了 } upstream local_iis_443 { server 后端IP1:443 max_fails=2 fail_timeout=10s weight=10; server 后端IP1:443 max_fails=2 fail_timeout=10s weight=10; keepalive 45; #防止出现nginx向后端发请求的时候连接打爆了 } server { listen 80; charset utf-8; location / { proxy_pass http://local_iis_80; } } server { listen 443 ssl http2; #支持http2 charset utf-8; ssl_certificate /data/cert/证书.crt; ssl_certificate_key /data/cert/证书.key; location / { proxy_pass https://local_iis_443; } }

 

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

sh 从源代码为Ubuntu 14.04安装最新的nginx

ubuntu16.04 yum报错:There are no enabled repos. Run “yum repolist all“ to see the repos you have.(代码片段

ubuntu安装nginx

ubuntu安装nginx

sh 在Ubuntu 14.04上从源代码安装Nginx,OpenSSL和ngx_pagespeed

Ubuntu16.04安装及配置nginx