ubuntu安装nginx

一、安装

1、更新软件包索引

若软件包近期已更新,该步骤可不操作。

sudo apt update

2、安装Nginx

sudo apt install nginx

3、启动Nginx服务

sudo systemctl start nginx.service 

4、查看nginx运行状态

sudo systemctl status nginx

image

 

输出里看到 Active: active (running) since ... 说明正在跑;
inactive (dead)failed 就是没起来或已崩溃。

5、设置开机启动:

sudo systemctl enable nginx.service 

二、修改配置文件

1、配置文件结构

  • /etc/nginx/nginx.conf: 主配置文件。
  • /etc/nginx/sites-available/: 存放每个站点的配置。
  • /etc/nginx/sites-enabled/: 存放链接到sites-available中活跃站点的符号链接。
  • /var/www/html: 默认文档根目录。

2、修改nginx.conf主配置文件

vim /etc/nginx/nginx.conf

user www-data;

worker_processes auto;

pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        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 TLSv1.3; # 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_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
        ##

################################################################
## 以下是新加路由
## 开放端口8081

    server {
      listen 8081;
      server_name localhost; # 若后续绑定域名再改


      location /xxx/ {
        proxy_pass http://127.0.0.1:8081/xxx/; # Java 服务
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
      }


      location @router {
        rewrite ^.*$ /index.html last;
      }
      #error_page 404 /404.html;


      # redirect server error pages to the static page /50x.html
      #
      error_page 500 502 503 504 /50x.html;
      location = /50x.html {
        root html;
      }
    }

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

3、重启nginx

nginx -t

验证nginx.conf是否规范,如下则验证通过。

image

nginx -s reload

执行重启命令。

 

posted @ 2025-09-24 11:30  pkuokuo  阅读(58)  评论(1)    收藏  举报