草庐IT

Ubuntu20.04安装配置Nginx

想当大佬的菜菜coder 2023-05-27 原文

由于在学习配置时,网上的教程比较杂乱,用时很久才做好一些基础配置,把流程记录一下方便和我一样的小白学习

本文写于2023.2.10,如果间隔太久,下述内容可能会失效,请另寻教程

仅包含基础教程,个人服务未涉及到负载均衡

  • 安装nginx
  • 配置静态服务器
  • 配置端口转发
  • 配置域名
  • 配置https

服务器: 阿里云ubuntu20.04

nginx版本: nginx/1.18.0 (Ubuntu)

1. 安装nginx

1.1 安装及常用命令

# 更新apt-get源
sudo apt-get update
# 安装
sudo apt-get install nginx
# 安装后将自动开启nginx服务,打开浏览器输入ip即可查看初始页面
# 查看安装版本
nginx -v
# 输出:nginx version: nginx/1.18.0 (Ubuntu)
# systemctl命令
# 查看状态
sudo systemctl status nginx
# 启动
sudo systemctl start nginx
# 停止
sudo systemctl stop nginx
# 重启
sudo systemctl restart nginx

注意:对nginx配置文件修改之后,都要重启nginx服务,加载修改后的配置文件

1.2 文件结构

# 查看文件结构
tree /etc/nginx
/etc/nginx
├── conf.d
├── fastcgi.conf
├── fastcgi_params
├── koi-utf
├── koi-win
├── mime.types
├── modules-available
├── modules-enabled
│   ├── 50-mod-http-image-filter.conf -> /usr/share/nginx/modules-available/mod-http-image-filter.conf
│   ├── 50-mod-http-xslt-filter.conf -> /usr/share/nginx/modules-available/mod-http-xslt-filter.conf
│   ├── 50-mod-mail.conf -> /usr/share/nginx/modules-available/mod-mail.conf
│   └── 50-mod-stream.conf -> /usr/share/nginx/modules-available/mod-stream.conf
├── nginx.conf
├── proxy_params
├── scgi_params
├── sites-available
│   └── default
├── sites-enabled
│   └── default -> /etc/nginx/sites-available/default
├── snippets
│   ├── fastcgi-php.conf
│   └── snakeoil.conf
├── uwsgi_params
└── win-utf

1.3 配置文件内容

nginx.conf (为了方便看,我删掉了初始内容中所有带注释的代码)

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 768;
}

http {

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	gzip on;

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

最关键的是下面两行引入,上面的代码含义目前我还没研究,用到再说

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

这两行的意思是:从conf.d中加载所有后缀为conf的文件,从sites-enabled中加载所有文件,均作为配置文件

sites-enabled文件我用不习惯,因此我注释掉了这行,使用conf.d做配置文件

在conf.d中添加static.conf

server {
    listen       80;
    server_name  localhost;

    charset utf-8; # 防止中文显示出现乱码

    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html; # 你的静态资源路径
        index  index.html index.htm;# 访问的文件为html, htm
    }
}

要注意的是,在/var/www/html目录中,文件的名字不是index.html,原名为index.nginx.debian.html,改成前者即可。

通过三处修改,完成从sites-enableconf.d的迁移

  • nginx.conf中注释掉include /etc/nginx/sites-enabled/*;
  • conf.d目录下新建static.conf,添加如上文件内容
  • 修改/var/www/html目录中的文件名为index.html
# 检查配置文件是否有误
nginx -t
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

# 重启服务
sudo systemctl restart nginx

2. 配置静态服务器

自行寻找一个网页做测试,上传到/var/www/html

我上传了之前写过的一个markdown在线编辑器,是一个文件夹,文件夹名为markdown

开始修改static.conf文件

server {
    listen       80;
    server_name  localhost;

    charset utf-8; # 防止中文显示出现乱码

    # 根据自己需要配置日志文件,可以单独配置,也可以全部放在/var/log/nginx的日志中
    #access_log  logs/host.access.log  main;

    location / {
        root   /var/www/html; # 你的静态资源路径
        index  index.html index.htm;# 访问的文件为html, htm
    }
    
    location /markdown {
        alias   /var/www/html/markdown; # 你的静态资源路径
        index  index.html index.htm;# 访问的文件为html, htm
    }
    
    # 后续如果有其他配置,模仿markdown的配置添加即可
    # location /example {
    #     alias   /var/www/html/example; # 你的静态资源路径
    #     index  index.html index.htm;# 访问的文件为html, htm
    # }
}

对于多个路径的配置: [1]

  • 使用root会将location后的markdown追加在路径的尾部,在访问时就会访问到/var/www/html/markdown/markdown

  • 使用alias则不会将location后的markdown追加在路径尾部,访问时就为正确路径/var/www/html/markdown

如果添加charset utf-8;后还存在乱码,强制刷新一下试试

输入IP/markdown查看配置结果

3. 配置端口转发

自行寻找一个服务做测试,开在非80端口即可,我开在了8822端口

注:要在服务器的安全组配置,打开对应端口

首先测试一下 IP:8822 能不能正常使用,可以使用说明服务成功启动在8822端口,进行后续配置。

server {
    listen       80;
    server_name  localhost;
 
    charset utf-8; # 防止中文显示出现乱码

	# 添加头部信息
    proxy_set_header  Cookie $http_cookie;
    proxy_set_header  X-Forwarded-Host $Host;
    proxy_set_header  proxy_set_Server  $Host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
	
    # 访问IP/eat,则会自动访问对应地址IP:8822
    location /eat/ {
        proxy_pass http://localhost:8822/;
    }
    
    # 后续如果有其他配置,模仿eat的配置添加即可
    # 访问IP/example,则会自动访问对应地址IP:port
    # location /example/ {
    #     proxy_pass http://localhost:port/;
    # }
}

输入IP/eat查看配置结果

4. 配置域名

我从阿里云购买了域名southyang.cn,将子域名demo.southyang.cn解析到服务器上

以端口转发为例:

server {
    listen       80;
    server_name  demo.southyang.cn;
 
    charset utf-8; # 防止中文显示出现乱码

	# 添加头部信息
    proxy_set_header  Cookie $http_cookie;
    proxy_set_header  X-Forwarded-Host $Host;
    proxy_set_header  proxy_set_Server  $Host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
	
    # 访问demo.southyang.cn/eat,则会自动访问对应地址IP:8822
    location /eat/ {
        proxy_pass http://localhost:8822/;
    }
    
    # 后续如果有其他配置,模仿eat的配置添加即可
    # 访问IP/example,则会自动访问对应地址IP:port
    # location /example/ {
    #     proxy_pass http://localhost:port/;
    # }
}

输入demo.southyang.cn/eat查看配置结果

5. 配置https

以端口转发为例: [2]

server {
    listen 80;
    server_name demo.southyang.cn;
    # 跳转https
    return 301 https://$host$request_uri;
}

server {
    listen 443   ssl http2;
    server_name  demo.southyang.cn;
 
    charset utf-8; # 防止中文显示出现乱码

	# 添加头部信息
    proxy_set_header  Cookie $http_cookie;
    proxy_set_header  X-Forwarded-Host $Host;
    proxy_set_header  proxy_set_Server  $Host;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    
    # 配置证书
    ssl_certificate 证书密钥地址
    ssl_certificate_key 证书公钥地址
    ssl_verify_client off;
    proxy_ssl_verify off;
	
    # 访问demo.southyang.cn/eat,则会自动访问对应地址IP:8822
    location /eat/ {
        proxy_pass http://localhost:8822/;
        proxy_redirect off;
    }
    
    # 后续如果有其他配置,模仿eat的配置添加即可
    # 访问IP/example,则会自动访问对应地址IP:port
    # location /example/ {
    #     proxy_pass http://localhost:port/;
    # }
}

输入demo.southyang.cn/eat查看配置结果

注: 目前我只用到了上述所列的内容,其他内容用到了再学

引用内容:

[1] https://blog.csdn.net/qq_39827677/article/details/113745095

[2] https://github.com/Mereithhh/van-nav

有关Ubuntu20.04安装配置Nginx的更多相关文章

  1. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  2. ruby - 完全离线安装RVM - 2

    我打算为ruby​​脚本创建一个安装程序,但我希望能够确保机器安装了RVM。有没有一种方法可以完全离线安装RVM并且不引人注目(通过不引人注目,就像创建一个可以做所有事情的脚本而不是要求用户向他们的bash_profile或bashrc添加一些东西)我不是要脚本本身,只是一个关于如何走这条路的快速指针(如果可能的话)。我们还研究了这个很有帮助的问题:RVM-isthereawayforsimpleofflineinstall?但有点误导,因为答案只向我们展示了如何离线在RVM中安装ruby。我们需要能够离线安装RVM本身,并查看脚本https://raw.github.com/wayn

  3. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  4. ruby - 如何为 emacs 安装 ruby​​-mode - 2

    我刚刚为fedora安装了emacs。我想用emacs编写ruby。为ruby​​提供代码提示、代码完成类型功能所需的工具、扩展是什么? 最佳答案 ruby-mode已经包含在Emacs23之后的版本中。不过,它也可以通过ELPA获得。您可能感兴趣的其他一些事情是集成RVM、feature-mode(Cucumber)、rspec-mode、ruby-electric、inf-ruby、rinari(用于Rails)等。这是我当前用于Ruby开发的Emacs配置:https://github.com/citizen428/emacs

  5. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  6. ruby-on-rails - 无法在centos上安装therubyracer(V8和GCC出错) - 2

    我正在尝试在我的centos服务器上安装therubyracer,但遇到了麻烦。$geminstalltherubyracerBuildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingtherubyracer:ERROR:Failedtobuildgemnativeextension./usr/local/rvm/rubies/ruby-1.9.3-p125/bin/rubyextconf.rbcheckingformain()in-lpthread...yescheckingforv8.h...no***e

  7. ruby-on-rails - 独立 ruby​​ 脚本的配置文件 - 2

    我有一个在Linux服务器上运行的ruby​​脚本。它不使用rails或任何东西。它基本上是一个命令行ruby​​脚本,可以像这样传递参数:./ruby_script.rbarg1arg2如何将参数抽象到配置文件(例如yaml文件或其他文件)中?您能否举例说明如何做到这一点?提前谢谢你。 最佳答案 首先,您可以运行一个写入YAML配置文件的独立脚本:require"yaml"File.write("path_to_yaml_file",[arg1,arg2].to_yaml)然后,在您的应用中阅读它:require"yaml"arg

  8. ruby - 通过 RVM (OSX Mountain Lion) 安装 Ruby 2.0.0-p247 时遇到问题 - 2

    我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search

  9. ruby - 如何在 Lion 上安装 Xcode 4.6,需要用 RVM 升级 ruby - 2

    我实际上是在尝试使用RVM在我的OSX10.7.5上更新ruby,并在输入以下命令后:rvminstallruby我得到了以下回复:Searchingforbinaryrubies,thismighttakesometime.Checkingrequirementsforosx.Installingrequirementsforosx.Updatingsystem.......Errorrunning'requirements_osx_brew_update_systemruby-2.0.0-p247',pleaseread/Users/username/.rvm/log/138121

  10. ruby - Fast-stemmer 安装问题 - 2

    由于fast-stemmer的问题,我很难安装我想要的任何ruby​​gem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=

随机推荐