原文链接:https://blog.csdn.net/qq_23832313/article/details/83578836
目录
ubuntu安装nginx
一、apt-get安装nginx
二、下载nginx包安装
在操作过程中有任何疑问,请留言,或者加群:高级java学习交流群(821605718)。
ubuntu安装nginx
目前支持两种安装方式,一种是apt-get的方式,另一种是根据包安装的方式
为方便我统一使用root用户
一、apt-get安装nginx
# 切换至root用户
sudo su root
apt-get install nginx
1
2
3
查看nginx是否安装成功
nginx -v
1
启动nginx
service nginx start
1
启动后,在网页重输入ip地址,即可看到nginx的欢迎页面。至此nginx安装成功
nginx文件安装完成之后的文件位置:
/usr/sbin/nginx:主程序
/etc/nginx:存放配置文件
/usr/share/nginx:存放静态文件
/var/log/nginx:存放日志
二、下载nginx包安装
由于上面已经安装了nginx,所以我们先卸载nginx。再重新上传nginx包,解压下载。有输入提示时,输入Y即可
卸载apt-get安装的nginx
# 彻底卸载nginx
apt-get --purge autoremove nginx
#查看nginx的版本号
nginx -v
1
2
3
4
安装依赖包
apt-get install gcc
apt-get install libpcre3 libpcre3-dev
apt-get install zlib1g zlib1g-dev
# Ubuntu14.04的仓库中没有发现openssl-dev,由下面openssl和libssl-dev替代
#apt-get install openssl openssl-dev
sudo apt-get install openssl
sudo apt-get install libssl-dev
1
2
3
4
5
6
7
安装nginx
cd /usr/local
mkdir nginx
cd nginx
wget http://nginx.org/download/nginx-1.13.7.tar.gz
tar -xvf nginx-1.13.7.tar.gz
1
2
3
4
5
编译nginx
# 进入nginx目录
/usr/local/nginx/nginx-1.13.7
# 执行命令
./configure
# 执行make命令
make
# 执行make install命令
make install
1
2
3
4
5
6
7
8
启动nginx
#进入nginx启动目录
cd /usr/local/nginx/sbin
# 启动nginx
./nginx
1
2
3
4
访问nginx
网页输入ip地址,访问成功,到此,nginx安装完毕
=============================================================
原文链接:https://blog.csdn.net/wjg8209/article/details/94899191
nginx介绍链接:https://www.cnblogs.com/knowledgesea/p/5175711.html
https://www.cnblogs.com/knowledgesea/p/5175711.html
https://www.cnblogs.com/knowledgesea/p/5199046.html
1. nginx介绍
2. nginx常用命令
验证配置是否正确: nginx -t
查看Nginx的详细的版本号:nginx -V
查看Nginx的简洁版本号:nginx -v
启动Nginx:start nginx
快速停止或关闭Nginx:nginx -s stop
正常停止或关闭Nginx:nginx -s quit
配置文件修改重装载命令:nginx -s reload
1
2
3
4
5
6
7
8
3. nginx配置
3.1 配置代码
#user nobody;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
#1 start
upstream linuxidc {
server localhost:7071;
server localhost:7072;
server localhost:7073;
}
server {
listen 7070;
server_name localhost;
location / {
# root C:/ngtest2;
# index index.html index.htm;
proxy_pass http://linuxidc;
}
}
# 1 end
#2 start
server {
listen 7071;
server_name localhost;
location / {
root C:/ngtest1;
# index index.html index.htm;
#proxy_pass https://tms;
#proxy_pass https://www.baidu.com/;
}
}
server {
listen 7072;
server_name localhost;
location / {
root C:/ngtest2;
# index index.html index.htm;
#proxy_pass https://tms;
}
}
server {
listen 7073;
server_name localhost;
location / {
root C:/ngtest3;
# index index.html index.htm;
#proxy_pass https://tms;
}
}
# 2 end
#3 start
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root C:\ngtest;
#index index.html index.htm;
#proxy_pass https://www.baidu.com/;
# }
location /baidu {
#root html;
#index index.html index.htm;
proxy_pass https://www.baidu.com/;
}
location /csdn {
#root html;
#index index.html index.htm;
proxy_pass https://www.csdn.net/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 3 end
}
}
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
84
85
86
87
88
89
90
91
3.2 代码说明及效果图
3.2.1 不同端口代理不同应用
#2 start
server {
listen 7071;
server_name localhost;
location / {
root C:/ngtest1;
# index index.html index.htm;
#proxy_pass https://tms;
#proxy_pass https://www.baidu.com/;
}
}
server {
listen 7072;
server_name localhost;
location / {
root C:/ngtest2;
# index index.html index.htm;
#proxy_pass https://tms;
}
}
server {
listen 7073;
server_name localhost;
location / {
root C:/ngtest3;
# index index.html index.htm;
#proxy_pass https://tms;
}
}
# 2 end
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
3.2.2 效果图
3.2.3 同一端口号代理不同应用
#3 start
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root C:\ngtest;
#index index.html index.htm;
#proxy_pass https://www.baidu.com/;
# }
location /baidu {
#root html;
#index index.html index.htm;
proxy_pass https://www.baidu.com/;
}
location /csdn {
#root html;
#index index.html index.htm;
proxy_pass https://www.csdn.net/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# 3 end
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
3.2.4 效果图
3.2.5 负载均衡
默认采用的轮询方式
#1 start
upstream linuxidc {
server localhost:7071;
server localhost:7072;
server localhost:7073;
}
server {
listen 7070;
server_name localhost;
location / {
# root C:/ngtest2;
# index index.html index.htm;
proxy_pass http://linuxidc;
}
}
# 1 end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
3.2.6 效果图
=================================================
参考链接:https://blog.csdn.net/junkaicool/article/details/93463067?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-6.nonecase
三、Nginx的常用配置
1.nginx.conf文件解读
nginx.conf 文件是Nginx总配置文件,在我们搭建服务器时经常调整的文件。
vi /etc/nginx/nginx.conf
1
下面是文件的详细注释
#运行用户,默认即是nginx,可以不进行设置
user nginx;
#Nginx进程,一般设置为和CPU核数一样
worker_processes 1;
#错误日志存放目录
error_log /var/log/nginx/error.log warn;
#进程pid存放位置
pid /var/run/nginx.pid;
events {
worker_connections 1024; # 单个后台进程的最大并发数
}
http {
include /etc/nginx/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 /var/log/nginx/access.log main; #nginx访问日志存放位置
sendfile on; #开启高效传输模式
#tcp_nopush on; #减少网络报文段的数量
keepalive_timeout 65; #保持连接的时间,也叫超时时间
#gzip on; #开启gzip压缩
include /etc/nginx/conf.d/*.conf; #包含的子配置项位置和文件
1
2
3
4
2.nginx的解析及反向代理配置
nginx的解析及反向代理配置全在在conf.d文件夹里面
cd /etc/nginx/conf.d
1
里面有一个默认的配置文件,default.conf,用vi default.conf编辑查看
server {
listen 80; #配置监听端口
server_name localhost; //配置域名
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html; #服务默认启动目录
index index.html index.htm; #默认访问文件
}
#error_page 404 /404.html; # 配置404页面
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html; #错误状态码的显示页面,配置后需要重启
location = /50x.html {
root /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
1
2
3
4
5
6
明白了这些配置项,我们知道我们的项目目录默认配置是放在/usr/share/nginx/html下,如果我们想修改的话就是修改这里,但是此处有个小坑,直接修改项目目录后会出现403错误,需要将/etc/nginx/nginx.conf中默认user: nginx改为user: root即可三、Nginx的常用命令
1.启动nginx
nginx
1
2.立即停止nginx
nginx -s stop
1
3.从容停止,比stop温和,即完成当前工作后停止
nginx -s quit
1
4.重启nginx,修改配置后执行
nginx -s reload
1
恭喜你,看到这,说明你已经学会了linux安装nginx环境及常用配置和命令