距离上次更新已经过了 788 天,文章所描述的內容可能已经发生变化,请留意
安装Nginx(首先保证有网) 准备 1 rpm -ivh http://nginx.org/packages/centos/6 /noarch/RPMS/nginx-release-centos-6-0 .el6.ngx.noarch.rpm
安装先决条件:
要设置yum存储库,创建名为/etc/yum.repos.d/nginx.repo的文件。 包括以下内容:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 [nginx -stable ] name=nginx stable repo baseurl=http://nginx.org/packages/centos/$releasever /$basearch / gpgcheck=0 enabled=1 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true [nginx -mainline ] name=nginx mainline repo baseurl=http://nginx.org/packages/mainline/centos/$releasever /$basearch / gpgcheck=0 enabled=0 gpgkey=https://nginx.org/keys/nginx_signing.key module_hotfixes=true
默认情况下,使用稳定nginx包的存储库。如果你想使用主线nginx包,运行以下命令:
1 yum-config-manager --enable nginx-mainline
要安装nginx,运行如下命令:
安装 启动服务 测试是否成功
1 2 3 4 service nginx start service nginx status http://IP
如果提示80端口被占用,可以进行如下修改(将nginx 端口号修改掉):
1 2 cd /etc/nginx/conf.d sudo vim default.conf
查看状态 还可以在web浏览器上进行访问 http://127.0.0.1
配置Nginx 配置文件 1 vi /etc/nginx/nginx.conf
目前nginx文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 user root; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; 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 /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65 ; types_hash_max_size 4096 ; include /etc/nginx/mime.types; default_type application/octet-stream ; include /etc/nginx/conf.d/*.conf; server { listen 80 ; server_name 192.168 .199.42 ; location / { proxy_pass http://127.0 .0.1 :8000 ; } location /statics/ { root /opt/cdpadmin/; } } }
测试的http访问:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 upstream django { server 127.0 .0.1 :8000 ; } server { listen 80 ; server_name 192.168 .199.42 ; charset utf-8 ; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream ; client_max_body_size 75 M; location /statics { alias /opt/cdpadmin/cdpapp/statics; } location / { uwsgi_pass django; include uwsgi_params; } }
测试的https访问:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 upstream django { server 127.0 .0.1 :8000 ; } server { listen 443 ssl; server_name 192.168 .199.42 ; ssl_certificate /etc/nginx/cert/ca.crt; ssl_certificate_key /etc/nginx/cert/ca.key; ssl_session_timeout 10 m; ssl_session_cache shared:SSL:10 m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 ; ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256 :HIGH:!aNULL:!MD5:!RC4:!DHE; ssl_prefer_server_ciphers on; charset utf-8 ; error_page 497 https://$host $request_uri ; location /statics { alias /opt/cdpadmin/cdpapp/statics; } location / { uwsgi_pass django; include uwsgi_params; } } server { listen 80 ; server_name 192.168 .199.42 ; charset utf-8 ; gzip on; gzip_types text/plain application/x-javascript text/css text/javascript application/x-httpd-php application/json text/json image/jpeg image/gif image/png application/octet-stream ; return 301 https://$host $request_uri ; client_max_body_size 75 M; location /statics { alias /opt/cdpadmin/cdpapp/statics; } location / { uwsgi_pass django; include uwsgi_params; } }
重启nginx 1 2 3 nginx -s reload nginx -s stop nginx
参考链接 1、nginx: Linux packages 2、centos6.7安装nginx