ubuntu环境nginx安装使用

方法一: 源码安装
1.官网地址下载对应版本
https://nginx.org/en/download.html

以 nginx-1.22.0.tar.gz 为例

使用xshell/xftp等工具上传到服务器节点

2.解压压缩包
tar -zxvf nginx-1.22.0.tar.gz
3.安装依赖
sudo apt install build-essential libpcre3 libpcre3-dev libssl-dev zlib1g zlib1g-dev
4.配置
cd nginx-1.22.0/

#http模式配置,--prefix指定安装目录
./configure --prefix=/usr/local/nginx
#或者
#带https模块配置
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module
#编译
make
#安装
make install

 方法二: 二进制安装

apt install nginx
启动命令
cd /usr/local/nginx/sbin
#启动命令
./nginx
#关闭命令
./nginx -s stop
# 重写加载配置文件
./nginx -s reload

验证安装版本

ps -ef | grep nginx
root      898799       1  0 01:11 ?        00:00:00 nginx: master process ./nginx
nobody    898800  898799  0 01:11 ?        00:00:00 nginx: worker process
root      898806  886568  0 01:12 pts/0    00:00:00 grep --color=auto nginx

./nginx -V
nginx version: nginx/1.22.0
built by gcc 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04) 
built with OpenSSL 1.1.1f  31 Mar 2020
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

 nginx首页页面:

云服务器放开出入方向规则(以华为云为例):

华为云==>安全组==>入方向规则 查看80端口是否开放,如未开放则添加;

防火墙查看80端口(以iptables防火墙为例):

#查看80端口
iptables -L -n | grep 80
#允许所有服务器访问指定端口80
iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT

首页默认端口号80, 网址: http://ip:80 或http://ip 

配置文件

默认配置文件 /usr/local/nginx/conf/nginx.conf

方向代理场修改的地方:

http==> server==>location

其中location中 root alias proxy_pass:

root: root指定的目录是上级目录,path匹配的整个路径会追加,即root+path;

alias: alias指定的目录必须带/,path匹配后面的内容会在alias指定的目录下查找,即alias+匹配到path路径后面的部分;

proxy_pass: path有无斜杠无影响,主要看proxy_pass有没有斜杠;proxy_pass没有的话,proxy_pass+path;有的话(包括端口和上下文都是一样的),proxy_pass+匹配到path路径后面的部分;

 

例:


#user nobody;

user root;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;


events {
worker_connections 1024;
}


http {
include 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 logs/access.log main;

sendfile on;
#tcp_nopush on;

#keepalive_timeout 0;
keepalive_timeout 65;

#gzip on;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

location /api {
proxy_pass http://127.0.0.1:8080/api;
index index.html index.htm;
}

#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;
}

# 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;
#}
}


# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;

# location / {
# root html;
# index index.html index.htm;
# }
#}


# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;

# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;

# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;

# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;

# location / {
# root html;
# index index.html index.htm;
# }
#}

}

注意: 第一行 "#user nobody;" 需变更为 "user root;", 使nginx的线程的用户为同一个用户名下;

保存退出,并重加载

cd /usr/local/nginx/sbin
./nginx -s reload
# 建立软连接
ln -s /usr/local/nginx/sbin/nginx /usr/bin/nginx

查看日志

默认日志路径: /usr/local/nginx/logs

(base) root@ecs-df00:/usr/local/nginx/logs# pwd
/usr/local/nginx/logs
(base) root@ecs-df00:/usr/local/nginx/logs# ll
total 20
drwxr-xr-x  2 root root 4096 Sep 23 01:11 ./
drwxr-xr-x 11 root root 4096 Sep 23 01:11 ../
-rw-r--r--  1 root root 2209 Sep 26 00:43 access.log
-rw-r--r--  1 root root 1835 Sep 26 00:43 error.log
-rw-r--r--  1 root root    7 Sep 23 01:11 nginx.pid

 

posted @ 2022-09-26 00:49  刀霸汉  阅读(999)  评论(0编辑  收藏  举报