安装Nginx(首先保证有网)

准备

1
rpm -ivh http://nginx.org/packages/centos/6/noarch/RPMS/nginx-release-centos-6-0.el6.ngx.noarch.rpm

安装先决条件:

1
yum install yum-utils

要设置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
yum install -y nginx

启动服务

测试是否成功

1
2
3
4
service nginx start
service nginx status
# 还可以在web浏览器上进行访问
http://IP

如果提示80端口被占用,可以进行如下修改(将nginx端口号修改掉):

1
2
cd /etc/nginx/conf.d
sudo vim default.conf

查看状态

1
service nginx status

还可以在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
# For more information on configuration, see:
# * Official English Documentation: http://nginx.org/en/docs/
# * Official Russian Documentation: http://nginx.org/ru/docs/

user root;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid; # centos6.7修改为 pid /var/run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
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;

# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;

server {
listen 80;
server_name 192.168.199.42;
location / {
# include uwsgi_params;
#uwsgi_pass 127.0.0.1:8000;
proxy_pass http://127.0.0.1:8000;
}

location /statics/ {
root /opt/cdpadmin/;
#proxy_pass http://127.0.0.1:8000/statics/;


}

}

# Settings for a TLS enabled server.
#
# server {
# listen 443 ssl http2;
# listen [::]:443 ssl http2;
# server_name _;
# root /usr/share/nginx/html;
#
# ssl_certificate "/etc/pki/nginx/server.crt";
# ssl_certificate_key "/etc/pki/nginx/private/server.key";
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 10m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
#
# # Load configuration files for the default server block.
# include /etc/nginx/default.d/*.conf;
#
# error_page 404 /404.html;
# location = /40x.html {
# }
#
# error_page 500 502 503 504 /50x.html;
# location = /50x.html {
# }
# }

}


测试的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 unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # 对于web端口套接字(我们将首先使用它)
}

# configuration of the server
server {
listen 80; # 站点将使用的端口
server_name 192.168.199.42; # 它将服务的域名,替换机器的IP地址或FQDN
charset utf-8; # Nginx编码
gzip on; # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
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 75M; # adjust to taste
# error_page 404 /404.html; # 错误页面
# error_page 500 502 503 504 /50x.html; # 错误页面

# location /media {
# alias /path/to/your/mysite/media; # 你的Django项目的media文件-根据需要修改
# }

location /statics {
alias /opt/cdpadmin/cdpapp/statics; # 你的Django项目的静态文件-根据需要修改
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
# uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
}
}

测试的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 unix:///path/to/your/mysite/mysite.sock; # for a file socket
server 127.0.0.1:8000; # 对于web端口套接字(我们将首先使用它)
}

# configuration of the server
server {
# 这里是你主要配置的部分 --begin--
listen 443 ssl; #网站运行的端口
server_name 192.168.199.42; #这里没有域名的话,就写你的服务器ip地址,使用ip来访问

ssl_certificate /etc/nginx/cert/ca.crt; #证书文件名称
ssl_certificate_key /etc/nginx/cert/ca.key; #私钥文件名称
ssl_session_timeout 10m;
ssl_session_cache shared:SSL:10m;
#请按照以下协议配置
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
#请按照以下套件配置,配置加密套件,写法遵循 openssl 标准。
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;
# 这里是你主要配置的部分 --end--

location /statics {
alias /opt/cdpadmin/cdpapp/statics; # 你的Django项目的静态文件-根据需要修改
}

location / {
uwsgi_pass django;
# uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
}
}

server {
listen 80; # 站点将使用的端口
server_name 192.168.199.42; # 它将服务的域名,替换机器的IP地址或FQDN
charset utf-8; # Nginx编码
gzip on; # 启用压缩,这个的作用就是给用户一个网页,比如3M压缩后1M这样传输速度就会提高很多
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; #访问http时跳转到https

# 最大上传大小
client_max_body_size 75M; # adjust to taste
# error_page 404 /404.html; # 错误页面
# error_page 500 502 503 504 /50x.html; # 错误页面

# location /media {
# alias /path/to/your/mysite/media; # 你的Django项目的media文件-根据需要修改
# }

location /statics {
alias /opt/cdpadmin/cdpapp/statics; # 你的Django项目的静态文件-根据需要修改
}

# Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
# uwsgi_connect_timeout 30; # 设置连接uWSGI超时时间
include uwsgi_params; # 导入一个Nginx模块他是用来和uWSGI进行通讯的
}
}

重启nginx

1
2
3
nginx -s reload  # 修改conf配置文件后重新加载
nginx -s stop # 停止nginx
nginx # 启动

参考链接

1、nginx: Linux packages
2、centos6.7安装nginx