在Ubuntu16.04.1上安装、配置、使用Nginx

实验环境

ubuntu16.04.1

怎么安装

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

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

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

使用locate nginx查看文件位置

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文件是配置文件,查看其内容

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

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

遂去探探究竟

发现

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

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

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文件里添加虚拟主机,使能够反向代理
server {
    listen       80;
    server_name  tomcat.abc.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
    }
}

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

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

  • 平滑的重启nginx
nginx -s reload
  • 快速的停止nginx
nginx -s stop
  • 优雅的停止nginx
nginx -s quit

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

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反向代理(端口转发)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值