nginx安裝

窃窃私语QAQ發表於2024-05-28

linux安裝nginx

手動安裝

官網下載倒數第二新的包:https://nginx.org/en/download.html

我這裡下載的是:nginx-1.24.0.tar.gz

安裝nginx需要很多的依賴,可以用該文章裡面的檢測依賴方式試試,反正是缺啥補啥好了(https://blog.csdn.net/weixin_40141628/article/details/137601683)

解壓檔案

tar -zxvf nginx-1.24.0.tar.gz

執行安裝

進入到解壓目錄

cd nginx-1.24.0

執行安裝

# --prfix 表示安裝的路徑
./configure --prefix=/usr/local --without-http_rewrite_module

執行編譯初始化

make && make install

啟動nginx

nginx

檢測是否成功

# 下面的兩個命令哪個都行
netstat -ntulp|grep 80
ps -ef|grep nginx

常用命令

# 啟動
nginx
# 停止
nginx -s stop
# 重啟
nginx -s reload

配置檔案

# 我這裡的配置檔案在/usr/local/conf中
cd /usr/local/conf

docker安裝

配置檔案的位置對應docker-compose.yml檔案中的對映位置: /etc/nginx/conf.d目錄下

# operation
map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
}


server {
    # html頁面
	listen 8015;
	listen [::]:8015;
	server_name 127.0.0.1:8015;
	location / {
		root /home/docker/nginx/web/screen;
		index aaaa.html bbb.html cc.html dd.html ee.html;
	}
	
}

server {
    # 某個後端服務
	listen 8014;
	listen [::]:8014;
	client_max_body_size 50m;
	server_name 127.0.0.1:8014;
	
	root /home/docker/nginx/web/;
	
	index index.html index.htm;
	
	location /freedom/demo/gateway/ {
	
		proxy_pass http://xxx.xx.xx.xx:8999;
		proxy_set_header Host $host;
           	proxy_set_header  X-Real-IP        $remote_addr;
            	proxy_set_header X-NginX-Proxy true;
		proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header Upgrade $http_upgrade;
		proxy_set_header Connection $connection_upgrade;
                # 這裡配置單個代理跨域,跨域配置
		add_header 'Access-Control-Allow-Origin' *;	
		add_header 'Access-Control-Allow-Credentials' 'true';
		add_header 'Access-Control-Allow-Methods' *;
		add_header 'Access-Control-Allow-Headers' *;
	
	}
	
}

docker-compose.yml檔案

version: "3"
services:
  nginx:
    image: nginx
    restart: always
    container_name: nginx
    privileged: true
    ports:
      - 80:80
      - 4443:443
      - 8014:8014
      - 8001:8001
      - 8003:8003
      - 8015:8015
      - 8013:8013
    volumes:
      - /etc/localtime:/etc/localtime:ro
      - ./log:/var/log/nginx
      - ./conf.d:/etc/nginx/conf.d
      - ./web:/home/docker/nginx/web
    network_mode: host