Nginx安装与简单使用

Nginx 是一个高性能的 Web 和反向代理服务器

实验环境

Ubuntu 16.04.1

安装

网上有很多安装的教程,自己摸索吧

我是通过 sudo apt-get install nginx 命令安装的

来吧,这里是安装的 官方教程

上述命令执行完,都干了什么?如何找到它的文件在哪呢?

使用 locate 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
84
85
86
87
88
ubuntu@VM-90-170-ubuntu:~$ locate nginx
/etc/nginx
/etc/default/nginx
/etc/init/nginx.conf
/etc/init.d/nginx
/etc/logrotate.d/nginx
/etc/nginx/conf.d
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi_params
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/nginx.conf
/etc/nginx/proxy_params
/etc/nginx/scgi_params
/etc/nginx/sites-available
/etc/nginx/sites-enabled
/etc/nginx/snippets
/etc/nginx/uwsgi_params
/etc/nginx/win-utf
/etc/nginx/sites-available/default
/etc/nginx/sites-enabled/default
/etc/nginx/snippets/fastcgi-php.conf
/etc/nginx/snippets/snakeoil.conf
/etc/rc0.d/K01nginx
/etc/rc1.d/K01nginx
/etc/rc2.d/S02nginx
/etc/rc3.d/S02nginx
/etc/rc4.d/S02nginx
/etc/rc5.d/S02nginx
/etc/rc6.d/K01nginx
/etc/systemd/system/multi-user.target.wants/nginx.service
/etc/ufw/applications.d/nginx
/lib/systemd/system/nginx.service
/usr/sbin/nginx
/usr/share/nginx
/usr/share/apport/package-hooks/source_nginx.py
/usr/share/doc/nginx
/usr/share/doc/nginx-common
/usr/share/doc/nginx-core
/usr/share/doc/nginx/changelog.Debian.gz
/usr/share/doc/nginx/copyright
/usr/share/doc/nginx-common/NEWS.Debian.gz
/usr/share/doc/nginx-common/README.Debian
/usr/share/doc/nginx-common/changelog.Debian.gz
/usr/share/doc/nginx-common/copyright
/usr/share/doc/nginx-core/changelog.Debian.gz
/usr/share/doc/nginx-core/copyright
/usr/share/lintian/overrides/nginx-common
/usr/share/lintian/overrides/nginx-core
/usr/share/nginx/html
/usr/share/nginx/html/index.html
/usr/share/vim/addons/ftdetect/nginx.vim
/usr/share/vim/addons/indent/nginx.vim
/usr/share/vim/addons/syntax/nginx.vim
/usr/share/vim/registry/nginx.yaml
/var/cache/apt/archives/nginx-common_1.10.3-0ubuntu0.16.04.2_all.deb
/var/cache/apt/archives/nginx-core_1.10.3-0ubuntu0.16.04.2_amd64.deb
/var/cache/apt/archives/nginx_1.10.3-0ubuntu0.16.04.2_all.deb
/var/lib/nginx
/var/lib/dpkg/info/nginx-common.conffiles
/var/lib/dpkg/info/nginx-common.config
/var/lib/dpkg/info/nginx-common.list
/var/lib/dpkg/info/nginx-common.md5sums
/var/lib/dpkg/info/nginx-common.postinst
/var/lib/dpkg/info/nginx-common.postrm
/var/lib/dpkg/info/nginx-common.preinst
/var/lib/dpkg/info/nginx-common.templates
/var/lib/dpkg/info/nginx-core.list
/var/lib/dpkg/info/nginx-core.md5sums
/var/lib/dpkg/info/nginx-core.postinst
/var/lib/dpkg/info/nginx-core.prerm
/var/lib/dpkg/info/nginx.list
/var/lib/dpkg/info/nginx.md5sums
/var/lib/nginx/body
/var/lib/nginx/fastcgi
/var/lib/nginx/proxy
/var/lib/nginx/scgi
/var/lib/nginx/uwsgi
/var/lib/systemd/deb-systemd-helper-enabled/nginx.service.dsh-also
/var/lib/systemd/deb-systemd-helper-enabled/multi-user.target.wants/nginx.service
/var/log/nginx
/var/log/nginx/access.log
/var/log/nginx/access.log.1
/var/log/nginx/access.log.2.gz
/var/log/nginx/error.log
/var/www/html/index.nginx-debian.html
ubuntu@VM-90-170-ubuntu:~$

可以看出

/etc/nginx/nginx.conf 文件是配置文件,查看其内容

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
ubuntu@VM-90-170-ubuntu:/etc/nginx$ pwd
/etc/nginx
ubuntu@VM-90-170-ubuntu:/etc/nginx$ cat nginx.conf
user www-data;
worker_processes auto;
pid /run/nginx.pid;
events {
worker_connections 768;
# 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;
# 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;
##
# Logging Settings
##
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
# gzip_vary on;
# gzip_proxied any;
# gzip_comp_level 6;
# gzip_buffers 16 8k;
# gzip_http_version 1.1;
# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
##
# Virtual Host Configs
##
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
#mail {
# # See sample authentication script at:
# # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
# # auth_http localhost/auth.php;
# # pop3_capabilities "TOP" "USER";
# # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
# server {
# listen localhost:110;
# protocol pop3;
# proxy on;
# }
#
# server {
# listen localhost:143;
# protocol imap;
# proxy on;
# }
#}
ubuntu@VM-90-170-ubuntu:/etc/nginx$

看到http下有incluede

1
2
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;

遂去探探究竟

发现

/etc/nginx/conf.d/ 文件夹下无内容

/etc/nginx/sites-enabled/有一个default文件,内容如下

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
ubuntu@VM-90-170-ubuntu:/etc/nginx/sites-enabled$ cat default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# http://wiki.nginx.org/Pitfalls
# http://wiki.nginx.org/QuickStart
# http://wiki.nginx.org/Configuration
#
# Generally, you will want to move this file somewhere, and start with a clean
# file but keep this around for reference. Or just disable in sites-enabled.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name _;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php7.0-cgi alone:
# fastcgi_pass 127.0.0.1:9000;
# # With php7.0-fpm:
# fastcgi_pass unix:/run/php/php7.0-fpm.sock;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
# 虚拟主机配置
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

配置

default文件里添加虚拟主机,使能够反向代理

1
2
3
4
5
6
7
8
server {
listen 80;
server_name tomcat.abc.com;
location / {
proxy_pass http://127.0.0.1:8080;
}
}

添加好了虚拟主机,重启nginx

以下操作要使终端在/etc/init.d/nginx下执行

平滑的重启nginx

1
nginx -s reload

快速的停止nginx

1
nginx -s stop

优雅的停止nginx

1
nginx -s quit

首页的文件在/var/www/html文件夹下,自己可以更改

下面是首页的默认内容

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
ubuntu@VM-90-170-ubuntu:/var/www/html$ pwd
/var/www/html
ubuntu@VM-90-170-ubuntu:/var/www/html$ cat index.nginx-debian.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
ubuntu@VM-90-170-ubuntu:/var/www/html$

反向代理前

反向代理前

反向代理后

反向代理后

参考链接

Nginx中文官方文档

If you think the content is useful to you.